Permalink
Browse files

[CommandLine] Several fixes to usage message generationg and error ha…

…ndling
  • Loading branch information...
1 parent c87ce20 commit 9b5908604a34d9bdfdaf2effbdd539a425ecdfb0 @Whiteknight committed Sep 3, 2012
View
3 src/commandline/ArgumentDef.winxed
@@ -22,6 +22,9 @@ namespace Rosella.CommandLine.ArgumentDef
*/
class Rosella.CommandLine.ArgumentDef
{
+ // TODO: Can we take positional aliases here in some way, with the rest of
+ // the named argument defs for unified processing and description generation?
+
var raw_defs; // The raw argument definitions from the user
var prepared_defs; // The prepared/processed argument definitions
var aliases; // Map of aliased argument names
View
11 src/commandline/ModeCondition.winxed
@@ -16,6 +16,13 @@ class Rosella.CommandLine.ModeCondition
function get_description(var sb)
{
}
+
+ function __get_flag_name(string name)
+ {
+ if (length(name) == 1)
+ return "-" + name;
+ return "--" + name;
+ }
}
// Condition that requires the presence of a given flag
@@ -39,7 +46,7 @@ class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondit
{
string flagname = self.flagname;
push(sb, " ");
- push(sb, flagname);
+ push(sb, self.__get_flag_name(flagname));
}
}
@@ -70,7 +77,7 @@ class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCo
string name = self.name;
string value = self.value;
push(sb, " ");
- push(sb, name);
+ push(sb, self.__get_flag_name(name));
push(sb, "=");
push(sb, value);
}
View
15 src/commandline/Program.winxed
@@ -66,23 +66,20 @@ class Rosella.CommandLine.Program
function run(var raw_args)
{
var args = new Rosella.CommandLine.Arguments(self);
+ var main_func;
try {
args.parse(raw_args, self.arg_defs);
+
+ var mode = self.__find_mode(args);
+ main_func = mode.main_function();
+
+ mode.alias_positionals(args);
} catch (e) {
say(e);
self.try_handle_error(args);
return;
}
- var mode = self.__find_mode(args);
- var main_func = mode.main_function();
- if (main_func == null) {
- self.try_handle_error(args);
- return;
- }
-
- mode.alias_positionals(args);
-
int exit_code = 0;
int has_ec = false;
Rosella.Parrot.try_report(function () {
View
11 src/commandline/ProgramMode.winxed
@@ -71,6 +71,9 @@ class Rosella.CommandLine.ProgramMode
// Get the main function to dispatch to.
function main_function()
{
+ var main_func = self.func;
+ if (main_func == null)
+ Rosella.Error.error("Main function for mode %s not set", self.name);
return self.func;
}
@@ -130,7 +133,7 @@ class Rosella.CommandLine.ProgramMode.Default : Rosella.CommandLine.ProgramMode
{
function Default() { self.name = ""; }
- function get_usage(string program_name, var sb)
+ function get_usage(string program_name, var sb = new 'StringBuilder')
{
var usage = self.usage;
if (usage != null) {
@@ -139,11 +142,15 @@ class Rosella.CommandLine.ProgramMode.Default : Rosella.CommandLine.ProgramMode
push(sb, " ");
push(sb, string(usage));
push(sb, "\n");
- } else {
+ }
+ else if (self.conditions != null) {
push(sb, "Usage: ");
push(sb, program_name);
+ for (var condition in self.conditions)
+ condition.get_description(sb);
push(sb, " <OPTIONS>\n");
}
+ return sb;
}
function require_flag(string name)
View
2 src/utilities/test_all_lib.winxed
@@ -12,7 +12,7 @@ function main[main](var args)
"'winxed' or 'nqp' or others if you have suitable templates installed.");
p.default_mode()
.set_function(test_all_main)
- .set_usage("<lang> <library> <folder>");
+ .require_positionals(false, "lang", "library", "folder");
p.add_mode("help").require_flag("help").set_function(function(var args) { say(p.get_help_message()); });
p.run(args);
}

0 comments on commit 9b59086

Please sign in to comment.