Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[CommandLine] Implement some routines to get help/usage messages for …

…the commandline.
  • Loading branch information...
commit 03cde946d94c6b87d32dcef0d41b48959f599676 1 parent 0631b95
@Whiteknight authored
View
7 src/commandline/Arguments.winxed
@@ -9,7 +9,6 @@ class Rosella.CommandLine.Arguments
var named_cache; // The cache of already-parsed args
var pos_cache; // All positional arguments
var raw_args; // The raw argument list
- var arg_defs; // Processed arg definitions
// Constructor. Take the list of arguments and do a fast presort on them.
function Arguments(var program)
@@ -126,12 +125,6 @@ class Rosella.CommandLine.Arguments
self.pos_cache = positionals;
self.remainder = remainders;
self.raw_args = args;
- self.arg_defs = arg_defs_obj;
- }
-
- function get_argument_definitions()
- {
- return self.arg_defs;
}
// Get the remaining args
View
43 src/commandline/Program.winxed
@@ -13,6 +13,7 @@ class Rosella.CommandLine.Program
var arg_defs; // Hash of all argument definitions
var on_error; // The error handler callback
var modes; // A hash of named program modes
+ var description; // Program help description
// Constructor
function Program(string program_name)
@@ -30,12 +31,20 @@ class Rosella.CommandLine.Program
function arguments(var raw_defs)
{
self.arg_defs = new Rosella.CommandLine.ArgumentDef(raw_defs);
+ return self;
+ }
+
+ function set_description(string desc)
+ {
+ self.description = desc;
+ return self;
}
// Set a function to execute on errors
function on_error(var e)
{
self.on_error = e;
+ return self;
}
// Add a new ProgramMode with the given name
@@ -100,4 +109,38 @@ class Rosella.CommandLine.Program
}
on_error(e);
}
+
+ function get_help_text()
+ {
+ 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");
+
+ 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");
+ }
+ }
+ if (self.description != null) {
+ string desc = self.description;
+ push(sb, desc);
+ push(sb, "\n");
+ }
+ push(sb, "\n");
+ return self.arg_defs.get_description(sb);
+ }
}
View
24 src/commandline/ProgramMode.winxed
@@ -13,9 +13,10 @@ class Rosella.CommandLine.ProgramMode
// TODO: Take a list of handles to use as the interpreter default during
// execution
- var name; // The name of the mode
- var func; // The function to call
- var conditions; // The conditions that determine if we use this mode
+ var name; // The name of the mode
+ var func; // The function to call
+ var conditions; // The conditions that determine if we use this mode
+ var usage; // Usage string for this mode
// Constructor. Each ProgramMode has a name
function ProgramMode(string name)
@@ -30,6 +31,20 @@ class Rosella.CommandLine.ProgramMode
return self;
}
+ function set_usage(string usage)
+ {
+ self.usage = usage;
+ return self;
+ }
+
+ function get_usage()
+ {
+ var usage = self.usage;
+ if (usage == null)
+ return "";
+ return string(usage);
+ }
+
// Called by the Program class internally. This function determines if the
// given list of arguments satisfies the list of required arguments for this
// ProgramMode. If so, the mode will be activated and executed
@@ -56,6 +71,7 @@ class Rosella.CommandLine.ProgramMode
self.conditions = [];
var condition = new Rosella.CommandLine.ModeCondition.FlagSet(name);
push(self.conditions, condition);
+ return self;
}
function require_value(string name, string value)
@@ -64,6 +80,7 @@ class Rosella.CommandLine.ProgramMode
self.conditions = [];
var condition = new Rosella.CommandLine.ModeCondition.ScalarValue(name, value);
push(self.conditions, condition);
+ return self;
}
function require_condition(var f)
@@ -72,5 +89,6 @@ class Rosella.CommandLine.ProgramMode
self.conditions = [];
var condition = new Rosella.CommandLine.ModeCondition.GenericFunc(f);
push(self.conditions, condition);
+ return self;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.