Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[CommandLine] add a rudimentary ability to require a certain number o…

…f positional arguments. Right now we have to give them names, but we can't look them up by name later
  • Loading branch information...
commit e1b5631b8e5c59fd12804d633640608dfb9e5b90 1 parent 0a3f33f
@Whiteknight authored
View
2  src/commandline/Arguments.winxed
@@ -9,7 +9,7 @@ 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;
+ var arg_defs; // The ArgumentDef object
// Constructor. Take the list of arguments and do a fast presort on them.
function Arguments(var program)
View
36 src/commandline/ModeCondition.winxed
@@ -101,3 +101,39 @@ class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCo
push(sb, desc);
}
}
+
+class Rosella.CommandLine.ModeCondition.RequiredPositionals : Rosella.CommandLine.ModeCondition
+{
+ var required_args;
+ var can_have_more;
+
+ function RequiredPositionals(int can_have_more, var args)
+ {
+ self.required_args = args;
+ self.can_have_more = can_have_more;
+ }
+
+ function is_satisfied(var args)
+ {
+ say(self.can_have_more);
+ say(elements(self.required_args));
+ say(elements(args.pos_cache));
+ Rosella.Dumper.dump(args.pos_cache);
+ if (self.can_have_more) {
+ return elements(args.pos_cache) >= elements(self.required_args);
+ } else {
+ return elements(args.pos_cache) == elements(self.required_args);
+ }
+ }
+
+ function get_description(var sb)
+ {
+ for (string arg in self.required_args) {
+ push(sb, " <");
+ push(sb, arg);
+ push(sb, ">");
+ }
+ if (self.can_have_more)
+ push(sb, " ...");
+ }
+}
View
20 src/commandline/ProgramMode.winxed
@@ -10,14 +10,13 @@ class Rosella.CommandLine.ProgramMode
// TODO: Take a list of handles to use as the interpreter default during
// execution
- // TODO: Add a mechanism to require a certain number of positional arguments
-
// TODO: Add an aliasing ability to give names to positional arguments
- 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
+ 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
+ var required_positionals; // Required positionsl
// Constructor. Each ProgramMode has a name
function ProgramMode(string name)
@@ -111,6 +110,15 @@ class Rosella.CommandLine.ProgramMode
push(self.conditions, condition);
return self;
}
+
+ function require_positionals(int can_have_more, var args [slurpy])
+ {
+ if (self.conditions == null)
+ self.conditions = [];
+ var condition = new Rosella.CommandLine.ModeCondition.RequiredPositionals(can_have_more, args);
+ push(self.conditions, condition);
+ return self;
+ }
}
// Default program mode, similar to a normal mode but with no preconditions
Please sign in to comment.
Something went wrong with that request. Please try again.