Skip to content
Browse files

[CommandLine] update Argument.t tests to match the new ArgumentDef cr…

…eation semantics. Add an ability to do simple positional argument aliasing with names.
  • Loading branch information...
1 parent 2476c29 commit b090efffc8d6bf1c3ba10c3ab390bcc2a6af5331 @Whiteknight committed
View
7 src/commandline/ArgumentDef.winxed
@@ -37,7 +37,7 @@ class Rosella.CommandLine.ArgumentDef
self.raw_defs = raw_defs;
}
- function get_definition(string arg_name)
+ function get_definition(var args, string arg_name)
{
:(var arg_defs, var aliases) = self.__get_definitions();
if (exists arg_defs[arg_name])
@@ -46,6 +46,11 @@ class Rosella.CommandLine.ArgumentDef
string alias = aliases[arg_name];
return alias, arg_defs[alias];
}
+
+ // If we have a parsed arg in the cache, but no def, just assume it
+ // was added by some other means.
+ if (args != null && exists args[arg_name])
+ return arg_name, "s";
Rosella.Error.error("Unknown argument '%s'", arg_name);
}
View
21 src/commandline/Arguments.winxed
@@ -74,7 +74,7 @@ class Rosella.CommandLine.Arguments
continue;
}
- :(arg_name, string def) = arg_defs.get_definition(arg_name);
+ :(arg_name, string def) = arg_defs.get_definition(null, arg_name);
switch(def) {
case "b":
if (single == false)
@@ -138,6 +138,17 @@ class Rosella.CommandLine.Arguments
self.arg_defs = arg_defs;
}
+ function alias_positionals(req_pos)
+ {
+ int num_req = elements(req_pos);
+ if (elements(self.pos_cache) < num_req)
+ Rosella.Error.error("Expected %d positionals to alias. Have %d instead", num_req, elements(self.pos_cache));
+ for (int i = 0; i < elements(req_pos); i++) {
+ string name = req_pos[i];
+ self.named_cache[name] = self.pos_cache[i];
+ }
+ }
+
// Get the remaining args
function remainder()
{
@@ -155,7 +166,7 @@ class Rosella.CommandLine.Arguments
function get_integer_keyed[vtable](string name)
{
- :(name, string def) = self.arg_defs.get_definition(name);
+ :(name, string def) = self.arg_defs.get_definition(self.named_cache, name);
if (def != "b" && def != "i")
Rosella.Error.error("Argument '%s' is not an integer or boolean", name);
if (exists self.named_cache[name])
@@ -165,7 +176,7 @@ class Rosella.CommandLine.Arguments
function get_string_keyed[vtable](string name)
{
- :(name, string def) = self.arg_defs.get_definition(name);
+ :(name, string def) = self.arg_defs.get_definition(self.named_cache, name);
if (def != "s")
Rosella.Error.error("Argument '%s' is not a string", name);
if (exists self.named_cache[name])
@@ -175,7 +186,7 @@ class Rosella.CommandLine.Arguments
function get_number_keyed[vtable](string name)
{
- :(name, string def) = self.arg_defs.get_definition(name);
+ :(name, string def) = self.arg_defs.get_definition(self.named_cache, name);
if (def != "f")
Rosella.Error.error("Argument '%s' is not a floating point number", name);
if (exists self.named_cache[name])
@@ -185,7 +196,7 @@ class Rosella.CommandLine.Arguments
function get_pmc_keyed[vtable](string name)
{
- :(name, string def) = self.arg_defs.get_definition(name);
+ :(name, string def) = self.arg_defs.get_definition(self.named_cache, name);
if (exists self.named_cache[name])
return self.named_cache[name];
return null;
View
4 src/commandline/ModeCondition.winxed
@@ -115,10 +115,6 @@ class Rosella.CommandLine.ModeCondition.RequiredPositionals : Rosella.CommandLin
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 {
View
2 src/commandline/Program.winxed
@@ -83,6 +83,8 @@ class Rosella.CommandLine.Program
return;
}
+ mode.alias_positionals(args);
+
int exit_code = 0;
int has_ec = false;
Rosella.Parrot.try_report(function () {
View
10 src/commandline/ProgramMode.winxed
@@ -113,12 +113,22 @@ class Rosella.CommandLine.ProgramMode
function require_positionals(int can_have_more, var args [slurpy])
{
+ if (self.required_positionals != null)
+ Rosella.Error.error("Cannot specify more than one list of required positionals");
if (self.conditions == null)
self.conditions = [];
var condition = new Rosella.CommandLine.ModeCondition.RequiredPositionals(can_have_more, args);
push(self.conditions, condition);
+ self.required_positionals = args;
return self;
}
+
+ function alias_positionals(var args)
+ {
+ var req_pos = self.required_positionals;
+ if (req_pos != null)
+ args.alias_positionals(self.required_positionals);
+ }
}
// Default program mode, similar to a normal mode but with no preconditions
View
32 t/commandline/Arguments.t
@@ -4,19 +4,21 @@ function create_new(var p_args [slurpy])
return new Rosella.CommandLine.Arguments(null);
}
-function create_arg_defs()
+function create_arg_defs(var raw_arg_defs = null)
{
- var raw_arg_defs = {
- "A" : "The A flag",
- "B=s" : "The B scalar",
- "C=[]" : "The C list",
- "D={}" : "The D hash",
- "fff" : "The F flag",
- "ggg=s" : "The G scalar",
- "hhh=[]" : "The H list",
- "iii={}" : "The I hash"
- };
- return new Rosella.CommandLine.ArgumentDef(raw_arg_defs);
+ if (raw_arg_defs == null) {
+ raw_arg_defs = {
+ "A" : "The A flag",
+ "B=s" : "The B scalar",
+ "C=[]" : "The C list",
+ "D={}" : "The D hash",
+ "fff" : "The F flag",
+ "ggg=s" : "The G scalar",
+ "hhh=[]" : "The H list",
+ "iii={}" : "The I hash"
+ };
+ }
+ return Rosella.CommandLine.ArgumentDef.get_argument_definition(raw_arg_defs);
}
class Test_Rosella_CommandLine_Arguments
@@ -296,7 +298,7 @@ class Test_Rosella_CommandLine_Arguments
function combination_args()
{
var obj = create_new();
- var arg_defs = new Rosella.CommandLine.ArgumentDef({
+ var arg_defs = create_arg_defs({
"A" : "A",
"B" : "B",
"C" : "C",
@@ -334,7 +336,7 @@ class Test_Rosella_CommandLine_Arguments
function arg_does_not_exist()
{
var obj = create_new();
- var arg_defs = new Rosella.CommandLine.ArgumentDef({
+ var arg_defs = create_arg_defs({
"A" : "A"
});
@@ -347,7 +349,7 @@ class Test_Rosella_CommandLine_Arguments
function unknown_args()
{
var obj = create_new();
- var arg_defs = new Rosella.CommandLine.ArgumentDef({
+ var arg_defs = create_arg_defs({
"A" : "A",
"B" : "B",
"C" : "C",

0 comments on commit b090eff

Please sign in to comment.
Something went wrong with that request. Please try again.