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.
* Integers and Floats are rendered as numbers.
* 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.
* For fields that are just one character in length use a single `-`. Use double
dashes `--` for fields that are longer than one character.
@ -48,10 +48,8 @@ let flags = {
"some/dir",
"some/other/dir",
],
log = {
debug = true,
format = "json",
},
"log.debug" = true,
"log.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/other/dir",
],
log = {
debug = true,
format = "json",
},
"log.debug" = true,
"log.format" = "json",
};
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.
for v in def.iter() {
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!(
"Skipping non primitive val in list for flag {}{}",
pfx, name
);
} else {
self.write_flag_name(pfx, name, w)?;
self.write(pfx, vref, w)?;
self.write_simple_value(vref, w)?;
}
}
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 {
&Val::Empty => {
// Empty is a noop.
@ -86,56 +86,47 @@ impl FlagConverter {
&Val::Str(ref s) => {
write!(w, "'{}' ", s)?;
}
&Val::List(ref _def) => {
eprintln!("Skipping List...");
&Val::List(_) | &Val::Tuple(_) | &Val::Func(_) | &Val::Env(_) | &Val::Module(_) => {
// This is ignored
eprintln!("Skipping {}...", v.type_name());
}
&Val::Tuple(ref flds) => {
}
Ok(())
}
fn write(&self, pfx: &str, flds: &Vec<(String, Rc<Val>)>, w: &mut Write) -> ConvertResult {
for &(ref name, ref val) in flds.iter() {
if let &Val::Empty = val.as_ref() {
self.write_flag_name(pfx, name, w)?;
continue;
}
match val.as_ref() {
&Val::Tuple(_) => {
let new_pfx = format!("{}{}{}", pfx, name, self.sep);
self.write(&new_pfx, val, w)?;
&Val::Tuple(_) | &Val::Module(_) | &Val::Func(_) | &Val::Env(_) => {
eprintln!("Skipping {} in flag output tuple.", val.type_name());
}
&Val::List(ref def) => {
self.write_list_flag(pfx, name, def, w)?;
}
_ => {
&Val::Boolean(_) | &Val::Empty | &Val::Float(_) | &Val::Int(_) | &Val::Str(_) => {
self.write_flag_name(pfx, name, w)?;
self.write(pfx, &val, 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(())
}
}
impl Converter for FlagConverter {
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(
"Flag outputs must be a tuple",
ErrorType::ConvertError,
)));
}
self.write("", &v, &mut w)
}
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:
- keys in a tuple are converted into the argument name.
- values in a tuple are converted into the argument value.
- NULL values are not emitted.
- lists expand out into an argument for each item in the list.
- values in a tuple are converted into the argument value with following exceptions:
- NULL values print out only the key name for the flag.
- lists expand out into an argument for each item in the list.
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 `.`.
e.g. {{foo = {{bar = 1, baz = 2}}}} becomes --foo.bar=1 --foo.baz=2
- Functions and Modules are ignored.
- Tuples, Functions and Modules are ignored.