Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[CommandLine] Fix automated help message generation

  • Loading branch information...
commit cccf82db9378dd7069bdb4e148b11456c9cbf0a0 1 parent 3583a25
@Whiteknight authored
View
13 src/commandline/ArgumentDef.winxed
@@ -24,15 +24,22 @@ class Rosella.CommandLine.ArgumentDef
function __get_description_hash(var raw_defs, var sb)
{
int max = 0;
+ var fixed_defs = {};
for (string arg in raw_defs) {
- int len = length(arg);
+ var parts = split("=", arg);
+ string arg_name = parts[0];
+ string prefix = (length(arg_name) == 1) ? "-" : "--";
+ arg_name = prefix + arg;
+ fixed_defs[arg_name] = raw_defs[arg];
+
+ int len = length(arg_name);
if (len > max)
max = len;
}
max += 4;
- for (string arg in raw_defs) {
- string desc = raw_defs[arg];
+ for (string arg in fixed_defs) {
+ string desc = fixed_defs[arg];
push(sb, " ");
// TODO: add dashes. -X and --xxx instead of X and xxx
View
32 src/commandline/ModeCondition.winxed
@@ -4,6 +4,10 @@ class Rosella.CommandLine.ModeCondition
{
return false;
}
+
+ function get_description(var sb)
+ {
+ }
}
class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondition
@@ -21,6 +25,13 @@ class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondit
int value = args[flagname];
return value;
}
+
+ function get_description(var sb)
+ {
+ string flagname = self.flagname;
+ push(sb, " ");
+ push(sb, flagname);
+ }
}
class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCondition
@@ -43,15 +54,27 @@ class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCo
return true;
return false;
}
+
+ function get_description(var sb)
+ {
+ string name = self.name;
+ string value = self.value;
+ push(sb, " ");
+ push(sb, name);
+ push(sb, "=");
+ push(sb, value);
+ }
}
class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCondition
{
var func;
+ var desc;
- function GenericFunc(var f)
+ function GenericFunc(var f, string desc)
{
self.func = f;
+ self.desc = desc;
}
function is_satisfied(var args)
@@ -59,4 +82,11 @@ class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCo
var f = self.func;
return f(args);
}
+
+ function get_description(var sb)
+ {
+ string desc = self.desc;
+ push(sb, " ");
+ push(sb, desc);
+ }
}
View
20 src/commandline/Program.winxed
@@ -20,7 +20,7 @@ class Rosella.CommandLine.Program
{
self.program_name = program_name;
self.modes = {};
- self.default_mode = new Rosella.CommandLine.ProgramMode("default");
+ self.default_mode = new Rosella.CommandLine.ProgramMode.Default();
self.on_error = function(var e) {
say("Error parsing arguments");
if (e != null)
@@ -115,25 +115,11 @@ class Rosella.CommandLine.Program
var sb = new 'StringBuilder';
string name = self.program_name;
- string usage = self.default_mode.get_usage();
- if (usage == null || usage == "")
- usage = "<OPTIONS>";
- push(sb, "Usage: ");
- push(sb, name);
- push(sb, " ");
- push(sb, usage);
- push(sb, "\n");
+ self.default_mode.get_usage(name, sb);
for (string modename in self.modes) {
var mode = self.modes[modename];
- usage = mode.get_usage();
- if (usage != null && usage != "") {
- push(sb, "Usage: ");
- push(sb, name);
- push(sb, " ");
- push(sb, usage);
- push(sb, "\n");
- }
+ mode.get_usage(name, sb);
}
if (self.description != null) {
string desc = self.description;
View
62 src/commandline/ProgramMode.winxed
@@ -37,14 +37,23 @@ class Rosella.CommandLine.ProgramMode
return self;
}
- function get_usage()
+ function get_usage(string program_name, var sb)
{
- // TODO: Can we try to autogenerate this from the list of conditions,
- // if we don't have one set?
var usage = self.usage;
- if (usage == null)
- return "";
- return string(usage);
+ if (usage != null) {
+ push(sb, " ");
+ push(sb, program_name);
+ push(sb, " ");
+ push(sb, string(usage));
+ push(sb, "\n");
+ }
+ else if (self.conditions != null) {
+ push(sb, " ");
+ push(sb, program_name);
+ for (var condition in self.conditions)
+ condition.get_description(sb);
+ push(sb, " <OPTIONS>\n");
+ }
}
// Called by the Program class internally. This function determines if the
@@ -94,3 +103,44 @@ class Rosella.CommandLine.ProgramMode
return self;
}
}
+
+class Rosella.CommandLine.ProgramMode.Default : Rosella.CommandLine.ProgramMode
+{
+ function Default() { self.name = ""; }
+
+ function get_usage(string program_name, var sb)
+ {
+ var usage = self.usage;
+ if (usage != null) {
+ push(sb, "Usage: ");
+ push(sb, program_name);
+ push(sb, " ");
+ push(sb, string(usage));
+ push(sb, "\n");
+ } else {
+ push(sb, "Usage: ");
+ push(sb, program_name);
+ push(sb, " <OPTIONS>\n");
+ }
+ }
+
+ function require_flag(string name)
+ {
+ Rosella.Error.error("May not add prerequisite conditions to the default program mode");
+ }
+
+ function require_value(string name, string value)
+ {
+ Rosella.Error.error("May not add prerequisite conditions to the default program mode");
+ }
+
+ function require_condition(var f)
+ {
+ Rosella.Error.error("May not add prerequisite conditions to the default program mode");
+ }
+
+ function can_accept(var args)
+ {
+ return true;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.