Skip to content

Commit db4624b

Browse files
committed
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) <file> <mime>` flag. Fixes #87 Related to #81
1 parent 8ac8d3b commit db4624b

4 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/mako/cli/lib/argparse.mako

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
if mc.media_params:
3838
upload_protocols = [mp.protocol for mp in mc.media_params]
3939
mode = docopt_mode(upload_protocols)
40-
args.append('-%s %s %s %s' % (UPLOAD_FLAG, mode, FILE_ARG, MIME_ARG))
40+
args.append('-%s %s <%s> <%s>' % (UPLOAD_FLAG, mode, FILE_ARG, MIME_ARG))
4141
# end upload handling
4242
4343
if mc.optional_props or parameters is not UNDEFINED:
4444
args.append('[-%s %s...]' % (PARAM_FLAG, '<%s>' % VALUE_ARG))
4545
# end paramters
4646
4747
if mc.response_schema or mc.m.get('supportsMediaDownload', False):
48-
args.append('[-%s %s]' % (OUTPUT_FLAG, OUT_ARG))
48+
args.append('[-%s <%s>]' % (OUTPUT_FLAG, OUT_ARG))
4949
# handle output
5050
%>\
5151
${util.program_name()} [options] ${mangle_subcommand(resource)} ${mangle_subcommand(method)} ${' '.join(args)}
@@ -226,5 +226,41 @@ let arg_data = [
226226
</%block>
227227
% endfor # end for each resource
228228
];
229+
230+
for &(main_command_name, ref subcommands) in &arg_data {
231+
let mut mcmd = SubCommand::new(main_command_name);
232+
for &(sub_command_name, ref desc, ref args) in subcommands {
233+
let mut scmd = SubCommand::new(sub_command_name);
234+
if let &Some(desc) = desc {
235+
scmd = scmd.about(desc);
236+
}
237+
for &(ref arg_name, ref flag, ref desc, ref required, ref multi) in args {
238+
let mut arg = Arg::with_name(match (arg_name, flag) {
239+
(&Some(an), _) => an,
240+
(_, &Some(f)) => f,
241+
_ => unreachable!(),
242+
});
243+
if let &Some(short_flag) = flag {
244+
arg = arg.short(short_flag);
245+
}
246+
if let &Some(desc) = desc {
247+
arg = arg.help(desc);
248+
}
249+
if arg_name.is_some() && flag.is_some() {
250+
arg = arg.takes_value(true);
251+
}
252+
if let &Some(required) = required {
253+
arg = arg.required(required);
254+
}
255+
if let &Some(multi) = multi {
256+
arg = arg.multiple(multi);
257+
}
258+
scmd = scmd.arg(arg);
259+
}
260+
mcmd = mcmd.subcommand(scmd);
261+
}
262+
app = app.subcommand(mcmd);
263+
}
264+
let matches = app.get_matches();
229265
</%block>
230266
</%def>

src/mako/cli/lib/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
DEBUG_FLAG = 'debug'
2121
DEBUG_AUTH_FLAG = 'debug-auth'
2222

23-
FILE_ARG = '<file>'
24-
MIME_ARG = '<mime>'
25-
OUT_ARG = '<out>'
23+
FILE_ARG = 'file'
24+
MIME_ARG = 'mime'
25+
OUT_ARG = 'out'
2626

2727
FIELD_SEP = '.'
2828

src/mako/cli/lib/engine.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
%>\
3030
mod cmn;
3131
use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
32-
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, make_subcommand};
32+
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError};
3333
3434
use std::default::Default;
3535
use std::str::FromStr;

src/rust/cli/cmn.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,6 @@ use std::default::Default;
1616

1717
const FIELD_SEP: char = '.';
1818

19-
20-
fn make_subcommand(command_name: &str, desc: Option<&str>,
21-
args: &Vec<(Option<&str>, Option<&str>, Option<&str>,
22-
Option<bool>, Option<bool>)>)
23-
-> App<'a, 'v, 'ab, 'u, 'h, 'ar> {
24-
// arg_name: Option<&str>,
25-
// short_name: Option<&str>,
26-
// help: Option<&str>,
27-
// % if flag is not None:
28-
// .takes_value(${rust_boolean(arg_name)})
29-
// required: Option<bool>,
30-
// multiple: Option<bool>
31-
SubCommand::new(command_name)
32-
}
33-
34-
3519
#[derive(Clone, Default)]
3620
pub struct FieldCursor(Vec<String>);
3721

0 commit comments

Comments
 (0)