1 /+
2               Copyright Elias Batek 2017 - 2018.
3      Distributed under the Boost Software License, Version 1.0.
4         (See accompanying file LICENSE_1_0.txt or copy at
5               https://www.boost.org/LICENSE_1_0.txt)
6  +/
7 module midigamepad.shell.commandline;
8 
9 import std.conv : ConvException, parse;
10 import std.file : exists;
11 import std.stdio;
12 
13 import midigamepad.lib.midi;
14 import midigamepad.lib.translation.mappingsfile;
15 import midigamepad.shell.program;
16 import midigamepad.shell.texts;
17 import midigamepad.shell.tui : launchTUI;
18 
19 /++
20     Launches MIDI GamePad based on the passed arguments
21  +/
22 int launchCommandline(string[] args)
23 {
24     if (args[1] == "--help" || args[1] == "/?")
25     {
26         // Show help
27         printHeader();
28         printHelp(args[0]);
29         return 0;
30     }
31     else if (args[1] == "--version" || args[1] == "-v")
32     {
33         // Show version
34         printHeader();
35         return 0;
36     }
37     else if (args.length >= 2)
38     {
39         uint selection;
40 
41         try
42         {
43             selection = args[1].parse!uint;
44         }
45         catch (ConvException)
46         {
47             // The 1st argument is not a valid (uint) device ID
48             writeln("Invalid device ID passed.");
49             return 1;
50         }
51 
52         const uint devicesCount = getMIDIInputDevicesCount();
53 
54         // Check whether there is a such a MIDI input device
55         if (selection >= devicesCount)
56         {
57             writeln("The passed device ID was out of range.");
58             return 1;
59         }
60 
61         // Check whether the specified mappings file exists
62         if (!args[2].exists)
63         {
64             writeln("The specified mappings file does not exists.");
65             return 1;
66         }
67 
68         MIDIDeviceInfo dev = getMIDIInputDeviceInfo(selection);
69         MappingsCollection mpc = void;
70 
71         try
72         {
73             mpc = parseFile(args[2]);
74         }
75         catch (MappingsParserException ex)
76         {
77             printMappingsParserException(ex);
78             return 1;
79         }
80 
81         // Launch the main IO processor
82         return runMIDIGamePad(dev, mpc);
83     }
84     else
85     {
86         writeln("No mappings file specified.");
87         return 1;
88     }
89 }