Skip to content

Commit 7d5276b

Browse files
committed
fix cli args handling
1 parent e6737e0 commit 7d5276b

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

source/app.d

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,11 @@ void main(string[] args)
4141
bool graphical, noSnapshot, confirm;
4242
ushort sshPort;
4343

44-
try
44+
// Check for help options first - if found anywhere, show help and exit
45+
foreach (arg; args[1 .. $])
4546
{
46-
auto options = getopt(
47-
args,
48-
"d", "disk", &diskPath,
49-
"c", "cpu", &cpu,
50-
"r", "ram", &ram,
51-
"g", "graphical", &graphical,
52-
"w", "write-mode", &noSnapshot,
53-
"p", "ssh-port", &sshPort,
54-
"l", "log-file", &logFile,
55-
"a", "arch", &arch,
56-
"confirm", &confirm,
57-
"help", {
47+
if (arg == "--help" || arg == "-h" || arg == "help")
48+
{
5849
writeln("Usage: qboot [options]");
5950
writeln("Options:");
6051
writeln(" -d, --disk <path> Path to the qcow2 disk image (required)");
@@ -66,14 +57,69 @@ void main(string[] args)
6657
writeln(" -l, --log-file <path> Path to the log file (default: ", config.logFile, ")");
6758
writeln(" -a, --arch <arch> Architecture (x86_64, ppc64le, s390x, aarch64) (default: ", config.arch, ")");
6859
writeln(" --confirm Show command and wait for keypress before starting");
69-
writeln(" --help Show this help message");
60+
writeln(" -h, --help Show this help message");
7061
return;
7162
}
63+
}
64+
65+
try
66+
{
67+
// Check for common mistakes with single-dash long options
68+
foreach (arg; args[1 .. $])
69+
{
70+
if (arg == "-confirm")
71+
{
72+
stderr.writeln(
73+
"Error: Long options require double dashes. Use '--confirm' instead of '-confirm'");
74+
return;
75+
}
76+
}
77+
78+
auto options = getopt(
79+
args,
80+
"d", "disk", &diskPath,
81+
"c", "cpu", &cpu,
82+
"r", "ram", &ram,
83+
"g", "graphical", &graphical,
84+
"w", "write-mode", &noSnapshot,
85+
"p", "ssh-port", &sshPort,
86+
"l", "log-file", &logFile,
87+
"a", "arch", &arch,
88+
"confirm", &confirm
7289
);
90+
91+
// Additional validation for non-numeric CPU/RAM values
92+
if (!cpu.empty)
93+
{
94+
try
95+
{
96+
cpu.to!int;
97+
}
98+
catch (ConvException e)
99+
{
100+
stderr.writeln("Error: Invalid CPU value '", cpu, "'. Please provide a numeric value (e.g., --cpu=2)");
101+
stderr.writeln("Hint: If you meant to use --confirm, use double dashes: --confirm");
102+
return;
103+
}
104+
}
105+
106+
if (!ram.empty)
107+
{
108+
try
109+
{
110+
ram.to!int;
111+
}
112+
catch (ConvException e)
113+
{
114+
stderr.writeln("Error: Invalid RAM value '", ram, "'. Please provide a numeric value (e.g., --ram=4)");
115+
return;
116+
}
117+
}
73118
}
74119
catch (Exception e)
75120
{
76121
stderr.writeln("Error: ", e.msg);
122+
stderr.writeln("Use --help to see available options.");
77123
return;
78124
}
79125

0 commit comments

Comments
 (0)