DEV: Stop using nested tuples in flag output.

Now that we have string manipulation facilities in the stdlib
that is a less useful and more confusing feature. This code removes
it.
This commit is contained in:
Jeremy Wall 2019-05-30 18:01:05 -05:00
parent 85f239a566
commit 0766d8058f
5 changed files with 39 additions and 55 deletions

View File

@ -32,7 +32,7 @@ rules for each type.
* Boolean values are translated to "true" and "false" repsectively. * Boolean values are translated to "true" and "false" repsectively.
* Integers and Floats are rendered as numbers. * Integers and Floats are rendered as numbers.
* Tuples are rendered as `--field value` pairs for each field. * Tuples are rendered as `--field value` pairs for each field.
* Nested tuples concatenate the field names to create the field. * Nested tuples are ignored.
* Nested Lists generate a new `--field listitem` pair for each item in the list. * Nested Lists generate a new `--field listitem` pair for each item in the list.
* For fields that are just one character in length use a single `-`. Use double * For fields that are just one character in length use a single `-`. Use double
dashes `--` for fields that are longer than one character. dashes `--` for fields that are longer than one character.
@ -48,10 +48,8 @@ let flags = {
"some/dir", "some/dir",
"some/other/dir", "some/other/dir",
], ],
log = { "log.debug" = true,
debug = true, "log.format" = "json",
format = "json",
},
} }
``` ```

View File

@ -1 +0,0 @@
--port 8080 --listen '0.0.0.0' --verbose --dir 'some/dir' --dir 'some/other/dir' --log.debug true --log.format 'json'

View File

@ -6,10 +6,8 @@ let args = {
"some/dir", "some/dir",
"some/other/dir", "some/other/dir",
], ],
log = { "log.debug" = true,
debug = true, "log.format" = "json",
format = "json",
},
}; };
out flags args; out flags args;

View File

@ -55,20 +55,20 @@ impl FlagConverter {
// first of all we need to make sure that each &Val is only a primitive type. // first of all we need to make sure that each &Val is only a primitive type.
for v in def.iter() { for v in def.iter() {
let vref = v.as_ref(); let vref = v.as_ref();
if vref.is_list() || vref.is_tuple() || vref.is_func() { if vref.is_list() || vref.is_tuple() || vref.is_func() || vref.is_module() {
eprintln!( eprintln!(
"Skipping non primitive val in list for flag {}{}", "Skipping non primitive val in list for flag {}{}",
pfx, name pfx, name
); );
} else { } else {
self.write_flag_name(pfx, name, w)?; self.write_flag_name(pfx, name, w)?;
self.write(pfx, vref, w)?; self.write_simple_value(vref, w)?;
} }
} }
return Ok(()); return Ok(());
} }
fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> ConvertResult { fn write_simple_value(&self, v: &Val, w: &mut Write) -> ConvertResult {
match v { match v {
&Val::Empty => { &Val::Empty => {
// Empty is a noop. // Empty is a noop.
@ -86,41 +86,31 @@ impl FlagConverter {
&Val::Str(ref s) => { &Val::Str(ref s) => {
write!(w, "'{}' ", s)?; write!(w, "'{}' ", s)?;
} }
&Val::List(ref _def) => { &Val::List(_) | &Val::Tuple(_) | &Val::Func(_) | &Val::Env(_) | &Val::Module(_) => {
eprintln!("Skipping List..."); // This is ignored
eprintln!("Skipping {}...", v.type_name());
} }
&Val::Tuple(ref flds) => { }
for &(ref name, ref val) in flds.iter() { Ok(())
if let &Val::Empty = val.as_ref() { }
self.write_flag_name(pfx, name, w)?;
continue; fn write(&self, pfx: &str, flds: &Vec<(String, Rc<Val>)>, w: &mut Write) -> ConvertResult {
} for &(ref name, ref val) in flds.iter() {
match val.as_ref() { if let &Val::Empty = val.as_ref() {
&Val::Tuple(_) => { self.write_flag_name(pfx, name, w)?;
let new_pfx = format!("{}{}{}", pfx, name, self.sep); continue;
self.write(&new_pfx, val, w)?; }
} match val.as_ref() {
&Val::List(ref def) => { &Val::Tuple(_) | &Val::Module(_) | &Val::Func(_) | &Val::Env(_) => {
self.write_list_flag(pfx, name, def, w)?; eprintln!("Skipping {} in flag output tuple.", val.type_name());
} }
_ => { &Val::List(ref def) => {
self.write_flag_name(pfx, name, w)?; self.write_list_flag(pfx, name, def, w)?;
self.write(pfx, &val, w)?; }
} &Val::Boolean(_) | &Val::Empty | &Val::Float(_) | &Val::Int(_) | &Val::Str(_) => {
} self.write_flag_name(pfx, name, w)?;
self.write_simple_value(val, w)?;
} }
}
&Val::Func(ref _def) => {
// This is ignored
eprintln!("Skipping macro...");
}
&Val::Env(ref _fs) => {
// This is ignored
eprintln!("Skipping env...");
}
&Val::Module(ref _def) => {
// This is ignored
eprintln!("Skipping module...");
} }
} }
Ok(()) Ok(())
@ -129,13 +119,14 @@ impl FlagConverter {
impl Converter for FlagConverter { impl Converter for FlagConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult { fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
if !v.is_tuple() { if let &Val::Tuple(ref flds) = v.as_ref() {
self.write("", flds, &mut w)
} else {
return Err(Box::new(BuildError::new( return Err(Box::new(BuildError::new(
"Flag outputs must be a tuple", "Flag outputs must be a tuple",
ErrorType::ConvertError, ErrorType::ConvertError,
))); )));
} }
self.write("", &v, &mut w)
} }
fn file_ext(&self) -> String { fn file_ext(&self) -> String {

View File

@ -3,10 +3,8 @@ Flags converts a tuple into a set of command line arguments for command line app
The flags are converted using the following rules: The flags are converted using the following rules:
- keys in a tuple are converted into the argument name. - keys in a tuple are converted into the argument name.
- values in a tuple are converted into the argument value. - values in a tuple are converted into the argument value with following exceptions:
- NULL values are not emitted. - NULL values print out only the key name for the flag.
- lists expand out into an argument for each item in the list. - lists expand out into an argument for each item in the list.
e.g. {{foo = [1, 2]}} becomes --foo=1 --foo=2 e.g. {{foo = [1, 2]}} becomes --foo=1 --foo=2
- tuples expand out into an argument with the key as a prefix separated by a `.`. - Tuples, Functions and Modules are ignored.
e.g. {{foo = {{bar = 1, baz = 2}}}} becomes --foo.bar=1 --foo.baz=2
- Functions and Modules are ignored.