From db4624b46728379393372be40b1ce731fe8f28b4 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 29 Apr 2015 16:49:02 +0200 Subject: [PATCH] feat(clap): parse structure and build App 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) ` flag. Fixes #87 Related to #81 --- src/mako/cli/lib/argparse.mako | 40 ++++++++++++++++++++++++++++++++-- src/mako/cli/lib/cli.py | 6 ++--- src/mako/cli/lib/engine.mako | 2 +- src/rust/cli/cmn.rs | 16 -------------- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/mako/cli/lib/argparse.mako b/src/mako/cli/lib/argparse.mako index d273bbb331e..6790ef07ebe 100644 --- a/src/mako/cli/lib/argparse.mako +++ b/src/mako/cli/lib/argparse.mako @@ -37,7 +37,7 @@ 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: @@ -45,7 +45,7 @@ # 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)} @@ -226,5 +226,41 @@ let arg_data = [ % 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(); \ No newline at end of file diff --git a/src/mako/cli/lib/cli.py b/src/mako/cli/lib/cli.py index a2a909c00e1..43c53ad43df 100644 --- a/src/mako/cli/lib/cli.py +++ b/src/mako/cli/lib/cli.py @@ -20,9 +20,9 @@ DEBUG_FLAG = 'debug' DEBUG_AUTH_FLAG = 'debug-auth' -FILE_ARG = '' -MIME_ARG = '' -OUT_ARG = '' +FILE_ARG = 'file' +MIME_ARG = 'mime' +OUT_ARG = 'out' FIELD_SEP = '.' diff --git a/src/mako/cli/lib/engine.mako b/src/mako/cli/lib/engine.mako index 48eafd7251b..8667bbc5479 100644 --- a/src/mako/cli/lib/engine.mako +++ b/src/mako/cli/lib/engine.mako @@ -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; diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/cmn.rs index 25feecc78d1..273aab86ae7 100644 --- a/src/rust/cli/cmn.rs +++ b/src/rust/cli/cmn.rs @@ -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, Option)>) - -> 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, - // multiple: Option - SubCommand::new(command_name) -} - - #[derive(Clone, Default)] pub struct FieldCursor(Vec);