Skip to content

Commit

Permalink
[CommandLine] Fix some on_error semantics. Differentiate between usag…
Browse files Browse the repository at this point in the history
…e messages and help messages. Misc cleanups
  • Loading branch information
awhitworth committed Aug 29, 2012
1 parent 9279d57 commit 0a3f33f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
42 changes: 21 additions & 21 deletions src/commandline/Program.winxed
Expand Up @@ -8,9 +8,6 @@
*/
class Rosella.CommandLine.Program
{
// TODO: Fix on_error semantics, so we aren't possibly passing an exception to
// a help function instead of an Arguments object.

// TODO: Add an ability to pass arguments to the main routine flag instead of
// in a single Arguments object.

Expand All @@ -27,11 +24,6 @@ class Rosella.CommandLine.Program
self.program_name = program_name;
self.modes = {};
self.default_mode = new Rosella.CommandLine.ProgramMode.Default();
self.on_error = function(var e) {
say("Error parsing arguments");
if (e != null)
say(e.message);
};
}

/* Public Methods
Expand Down Expand Up @@ -76,12 +68,18 @@ class Rosella.CommandLine.Program
// TODO: Take some optional parameters, such as handles to use for
// overriding standard handles, other options.
var args = new Rosella.CommandLine.Arguments(self);
args.parse(raw_args, self.arg_defs);
try {
args.parse(raw_args, self.arg_defs);
} 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(null);
self.try_handle_error(args);
return;
}

Expand All @@ -96,17 +94,23 @@ class Rosella.CommandLine.Program
exit(exit_code);
}

function get_help_text()
function get_usage_text(var sb = new 'StringBuilder')
{
var sb = new 'StringBuilder';
string name = self.program_name;

self.default_mode.get_usage(name, sb);

for (string modename in self.modes) {
var mode = self.modes[modename];
mode.get_usage(name, sb);
}
return sb;
}

function get_help_text()
{
var sb = new 'StringBuilder';
self.get_usage_text(sb);
push(sb, "\n");
if (self.description != null) {
string desc = self.description;
push(sb, desc);
Expand All @@ -130,16 +134,12 @@ class Rosella.CommandLine.Program
}

// On error, try to execute the error handler, if any.
function try_handle_error(var e)
function try_handle_error(var args)
{
var on_error = self.on_error;
if (on_error == null) {
if (e == null) {
// If we fail to dispatch, just dump the usage message
say(self.get_help_text());
} else
throw e;
}
on_error(e);
say(self.get_usage_text());
} else
on_error(args);
}
}
14 changes: 1 addition & 13 deletions src/utilities/test_all_lib.winxed
Expand Up @@ -13,8 +13,7 @@ function main[main](var args)
p.default_mode()
.set_function(test_all_main)
.set_usage("<lang> <library> <folder>");
p.add_mode("help").require_flag("help").set_function(usage_and_exit);
p.on_error(usage_and_exit);
p.add_mode("help").require_flag("help").set_function(function(var args) { say(p.get_help_message()); });
p.run(args);
}

Expand Down Expand Up @@ -59,17 +58,6 @@ function test_all_main(var args)
});
}

// Help handler. Show usage information on --help or error
function usage_and_exit(var args)
{
if (__DEBUG__ && args instanceof 'Exception') {
say(args.message);
for (string bt in args.backtrace_strings())
say(bt);
}
say(args.program.get_help_text());
}

// Get the Directory object for the given namespace
function get_namespace_directory(var dir, string ns)
{
Expand Down

0 comments on commit 0a3f33f

Please sign in to comment.