Skip to content

Commit

Permalink
feat(clap): parse structure and build App
Browse files Browse the repository at this point in the history
We are currently setting everything up at runtime, and manage to get
nearly all information into it, except for the more complex
`-u (simple|resumable) <file> <mime>` flag.

Fixes #87
Related to #81
  • Loading branch information
Byron committed Apr 29, 2015
1 parent 8ac8d3b commit db4624b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
40 changes: 38 additions & 2 deletions src/mako/cli/lib/argparse.mako
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
if mc.media_params:
upload_protocols = [mp.protocol for mp in mc.media_params]
mode = docopt_mode(upload_protocols)
args.append('-%s %s %s %s' % (UPLOAD_FLAG, mode, FILE_ARG, MIME_ARG))
args.append('-%s %s <%s> <%s>' % (UPLOAD_FLAG, mode, FILE_ARG, MIME_ARG))
# end upload handling
if mc.optional_props or parameters is not UNDEFINED:
args.append('[-%s %s...]' % (PARAM_FLAG, '<%s>' % VALUE_ARG))
# end paramters
if mc.response_schema or mc.m.get('supportsMediaDownload', False):
args.append('[-%s %s]' % (OUTPUT_FLAG, OUT_ARG))
args.append('[-%s <%s>]' % (OUTPUT_FLAG, OUT_ARG))
# handle output
%>\
${util.program_name()} [options] ${mangle_subcommand(resource)} ${mangle_subcommand(method)} ${' '.join(args)}
Expand Down Expand Up @@ -226,5 +226,41 @@ let arg_data = [
</%block>
% endfor # end for each resource
];
for &(main_command_name, ref subcommands) in &arg_data {
let mut mcmd = SubCommand::new(main_command_name);
for &(sub_command_name, ref desc, ref args) in subcommands {
let mut scmd = SubCommand::new(sub_command_name);
if let &Some(desc) = desc {
scmd = scmd.about(desc);
}
for &(ref arg_name, ref flag, ref desc, ref required, ref multi) in args {
let mut arg = Arg::with_name(match (arg_name, flag) {
(&Some(an), _) => an,
(_, &Some(f)) => f,
_ => unreachable!(),
});
if let &Some(short_flag) = flag {
arg = arg.short(short_flag);
}
if let &Some(desc) = desc {
arg = arg.help(desc);
}
if arg_name.is_some() && flag.is_some() {
arg = arg.takes_value(true);
}
if let &Some(required) = required {
arg = arg.required(required);
}
if let &Some(multi) = multi {
arg = arg.multiple(multi);
}
scmd = scmd.arg(arg);
}
mcmd = mcmd.subcommand(scmd);
}
app = app.subcommand(mcmd);
}
let matches = app.get_matches();
</%block>
</%def>
6 changes: 3 additions & 3 deletions src/mako/cli/lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
DEBUG_FLAG = 'debug'
DEBUG_AUTH_FLAG = 'debug-auth'

FILE_ARG = '<file>'
MIME_ARG = '<mime>'
OUT_ARG = '<out>'
FILE_ARG = 'file'
MIME_ARG = 'mime'
OUT_ARG = 'out'

FIELD_SEP = '.'

Expand Down
2 changes: 1 addition & 1 deletion src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
%>\
mod cmn;
use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, make_subcommand};
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError};
use std::default::Default;
use std::str::FromStr;
Expand Down
16 changes: 0 additions & 16 deletions src/rust/cli/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ use std::default::Default;

const FIELD_SEP: char = '.';


fn make_subcommand(command_name: &str, desc: Option<&str>,
args: &Vec<(Option<&str>, Option<&str>, Option<&str>,
Option<bool>, Option<bool>)>)
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
// arg_name: Option<&str>,
// short_name: Option<&str>,
// help: Option<&str>,
// % if flag is not None:
// .takes_value(${rust_boolean(arg_name)})
// required: Option<bool>,
// multiple: Option<bool>
SubCommand::new(command_name)
}


#[derive(Clone, Default)]
pub struct FieldCursor(Vec<String>);

Expand Down

0 comments on commit db4624b

Please sign in to comment.