Line data Source code
1 : #pragma once
2 : #include <iostream>
3 : #include <string>
4 :
5 : namespace Amplitron {
6 :
7 : //Container for all terminal args
8 40 : struct CliOptions {
9 20 : bool exit_early = false;
10 20 : bool is_headless = false;
11 20 : int exit_code = 0;
12 : std::string preset_path;
13 : std::string input_device;
14 : std::string output_device;
15 : std::string exit_reason;
16 : };
17 :
18 60 : inline CliOptions handle_cli_args(int argc, char* argv[]) {
19 60 : CliOptions options;
20 67 : auto has_value_token = [&](int idx) {
21 27 : return idx < argc && argv[idx] != nullptr && argv[idx][0] != '-';
22 40 : };
23 93 : for (int i = 1; i < argc; ++i) {
24 69 : std::string arg = argv[i];
25 69 : if (arg == "--help" || arg == "-h") {
26 8 : std::cout << "Usage: amplitron [options]\n\n"
27 4 : << "Options:\n"
28 4 : << " -h, --help Show this help message and exit\n"
29 4 : << " -v, --version Show version information and exit\n"
30 4 : << "\nAudio devices are configured via File -> Settings in the GUI.\n"
31 12 : << "Visit https://github.com/sudip-mondal-2002/Amplitron for docs.\n";
32 12 : options.exit_early=true;
33 12 : options.exit_reason="Help Requested";
34 8 : return options;
35 : }
36 57 : else if (arg == "--version" || arg == "-v") {
37 12 : std::cout << "Amplitron v1.0\n";
38 12 : options.exit_early=true;
39 12 : options.exit_reason="Version Requested";
40 8 : return options;
41 : }
42 45 : else if(arg == "--headless" || arg == "--no-gui"){
43 12 : options.is_headless = true;
44 4 : }
45 : //i+1<argc prevents segfault if user does not provide sufficient arguments
46 33 : else if(arg == "--preset"){
47 15 : if(has_value_token(i + 1)){
48 9 : options.preset_path = argv[++i];
49 3 : } else{
50 6 : std::cerr << "Error: --preset requires a file path argument.\n";
51 6 : options.exit_early = true;
52 6 : options.exit_code = 1;
53 6 : options.exit_reason="Preset Field Empty";
54 4 : return options;
55 : }
56 3 : }
57 18 : else if(arg == "--input"){
58 6 : if(has_value_token(i + 1)){
59 3 : options.input_device = argv[++i];
60 1 : } else{
61 3 : std::cerr << "Error: --input requires a device name argument.\n";
62 3 : options.exit_early = true;
63 3 : options.exit_code = 1;
64 3 : options.exit_reason="Input Field Empty.";
65 2 : return options;
66 : }
67 1 : }
68 12 : else if(arg == "--output"){
69 6 : if(has_value_token(i + 1)){
70 13 : options.output_device = argv[++i];
71 1 : }else{
72 3 : std::cerr << "Error: --output requires a device name argument.\n";
73 3 : options.exit_early = true;
74 3 : options.exit_code = 1;
75 14 : options.exit_reason="Output Field Empty";
76 2 : return options;
77 : }
78 1 : }
79 69 : }
80 :
81 : //Check: can't run headless without a preset.
82 24 : if(options.is_headless && options.preset_path.empty()){
83 6 : std::cerr << "Error: --headless/--no-gui mode requires a --preset <path> argument.\n";
84 6 : options.exit_early = true;
85 6 : options.exit_code = 1;
86 24 : options.exit_reason="Insufficient Arguments(no --preset)";
87 2 : }
88 16 : return options;
89 20 : }
90 :
91 : } // namespace Amplitron
|