Skip to content

Commit 5320a48

Browse files
committed
fix(clap): tweaks to make youtube3 work
Mainly minor cleanup, and handling of generator branches that didn't show up in smaller APIs that were used during the first steps. related to #81
1 parent bac4e1a commit 5320a48

4 files changed

Lines changed: 21 additions & 33 deletions

File tree

src/mako/cli/lib/argparse.mako

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from util import (put_and, supports_scopes, api_index, indent_by, enclose_in)
66
from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG,
77
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
8-
CONFIG_DIR_FLAG, KEY_VALUE_ARG, to_docopt_arg, DEBUG_FLAG, DEBUG_AUTH_FLAG)
8+
CONFIG_DIR_FLAG, KEY_VALUE_ARG, to_docopt_arg, DEBUG_FLAG, DEBUG_AUTH_FLAG, MODE_ARG)
99
1010
def rust_boolean(v):
1111
return v and 'true' or 'false'
@@ -64,7 +64,7 @@ Configuration:
6464
% if supports_scopes(auth):
6565
--${SCOPE_FLAG} <url>
6666
Specify the authentication a method should be executed in. Each scope
67-
requires the user to grant this application permission to use it.
67+
requires the user to grant this application permission to use it.
6868
If unset, it defaults to the shortest scope url for a particular method.
6969
% endif scopes
7070
--${CONFIG_DIR_FLAG} <folder>
@@ -187,12 +187,11 @@ let arg_data = [
187187
args.append((
188188
UPLOAD_FLAG,
189189
"Specify which file to upload",
190-
"mode",
191-
False,
190+
MODE_ARG,
191+
True,
192192
False,
193193
upload_protocols
194194
))
195-
## args.append('-%s %s %s %s' % (UPLOAD_FLAG, mode, FILE_ARG, MIME_ARG))
196195
# end upload handling
197196
198197
if mc.optional_props or parameters is not UNDEFINED:
@@ -284,12 +283,12 @@ for &(main_command_name, ref subcommands) in arg_data.iter() {
284283
285284
scmd = scmd.arg(Arg::with_name("file")
286285
.short("f")
287-
.required(false)
286+
.required(true)
288287
.help("The file to upload")
289288
.takes_value(true));
290289
scmd = scmd.arg(Arg::with_name("mime")
291290
.short("m")
292-
.required(false)
291+
.required(true)
293292
.help("The file's mime time, like 'application/octet-stream'")
294293
.takes_value(true));
295294
}
@@ -299,6 +298,5 @@ for &(main_command_name, ref subcommands) in arg_data.iter() {
299298
}
300299
app = app.subcommand(mcmd);
301300
}
302-
let matches = app.get_matches();
303301
</%block>
304302
</%def>

src/mako/cli/lib/cli.py

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

23+
MODE_ARG = 'mode'
2324
FILE_ARG = 'file'
2425
MIME_ARG = 'mime'
2526
OUT_ARG = 'out'
@@ -91,12 +92,12 @@ def mangle_subcommand(name):
9192
def ident(name):
9293
return mangle_subcommand(name).replace('-', '_')
9394

94-
# Similar to cmd_ident, but for arguments
95-
def arg_ident(name):
96-
return opt_value(name)
95+
# Return a required value in Rust, using unwrap()
96+
def req_value(name):
97+
return 'opt.value_of("' + mangle_subcommand(name) + '").unwrap()'
9798

98-
def opt_value(name, opt='opt'):
99-
return opt + '.value_of("' + mangle_subcommand(name) + '").unwrap_or("")'
99+
def opt_value(name, opt='opt', default=''):
100+
return opt + '.value_of("' + mangle_subcommand(name) + ('").unwrap_or("%s")' % default)
100101

101102
def application_secret_path(program_name):
102103
return program_name + '-secret.json'

src/mako/cli/lib/engine.mako

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
ADD_SCOPE_FN, TREF)
66
from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG,
77
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
8-
call_method_ident, arg_ident, POD_TYPES, opt_value, ident, JSON_TYPE_VALUE_MAP,
8+
call_method_ident, POD_TYPES, opt_value, ident, JSON_TYPE_VALUE_MAP,
99
KEY_VALUE_ARG, to_cli_schema, SchemaEntry, CTYPE_POD, actual_json_type, CTYPE_MAP, CTYPE_ARRAY,
10-
application_secret_path, DEBUG_FLAG, DEBUG_AUTH_FLAG, CONFIG_DIR_FLAG)
10+
application_secret_path, DEBUG_FLAG, DEBUG_AUTH_FLAG, CONFIG_DIR_FLAG, req_value, MODE_ARG)
1111
1212
v_arg = '<%s>' % VALUE_ARG
1313
SOPT = 'self.opt'
@@ -172,7 +172,7 @@ ${self._request_value_impl(c, request_cli_schema, prop_name, request_prop_type)}
172172
% elif p.type != 'string':
173173
% if p.get('repeated', False):
174174
let ${prop_name}: Vec<${prop_type} = Vec::new();
175-
for (arg_id, arg) in opt.values_of().unwrap_or(Vec::new()).iter().enumerate() {
175+
for (arg_id, arg) in opt.values_of("${mangle_subcommand(p.name)}").unwrap_or(Vec::new()).iter().enumerate() {
176176
${prop_name}.push(arg_from_str(&arg, err, "<${mangle_subcommand(p.name)}>", arg_id), "${p.type}"));
177177
}
178178
% else:
@@ -197,7 +197,7 @@ let mut download_mode = false;
197197
% endif
198198
let mut call = self.hub.${mangle_ident(resource)}().${mangle_ident(method)}(${', '.join(call_args)});
199199
% if handle_props:
200-
for parg in opt.values_of("${ident(VALUE_ARG)}").unwrap_or(Vec::new()).iter() {
200+
for parg in opt.values_of("${VALUE_ARG}").unwrap_or(Vec::new()).iter() {
201201
let (key, value) = parse_kv_arg(&*parg, err, false);
202202
match key {
203203
% for p in optional_props:
@@ -253,21 +253,9 @@ ${value_unwrap}\
253253
}
254254
% endif # handle call parameters
255255
% if mc.media_params:
256-
let protocol =
257-
% for p in mc.media_params:
258-
% if loop.first:
259-
if \
260-
% else:
261-
} else if \
262-
% endif
263-
${opt_value(p.protocol)} {
264-
"${p.protocol}"
265-
% endfor # each media param
266-
} else {
267-
unreachable!()
268-
};
269-
let mut input_file = input_file_from_opts(&${opt_value(FILE_ARG)}, err);
270-
let mime_type = input_mime_from_opts(&${opt_value(MIME_ARG)}, err);
256+
let protocol = ${req_value(MODE_ARG)};
257+
let mut input_file = input_file_from_opts(${req_value(FILE_ARG)}, err);
258+
let mime_type = input_mime_from_opts(${req_value(MIME_ARG)}, err);
271259
% else:
272260
let protocol = "${STANDARD}";
273261
% endif # support upload
@@ -376,7 +364,7 @@ if dry_run {
376364
%>\
377365
let mut ${request_prop_name} = api::${request_prop_type}::default();
378366
let mut field_cursor = FieldCursor::default();
379-
for kvarg in ${opt_value(KEY_VALUE_ARG)}.iter() {
367+
for kvarg in opt.values_of("${KEY_VALUE_ARG}").unwrap_or(Vec::new()).iter() {
380368
let last_errc = err.issues.len();
381369
let (key, value) = parse_kv_arg(&*kvarg, err, false);
382370
let mut temp_cursor = field_cursor.clone();

src/mako/cli/main.rs.mako

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ${engine.new(c)}\
3434

3535
fn main() {
3636
${argparse.new(c) | indent_all_but_first_by(1)}\
37+
let matches = app.get_matches();
3738

3839
let debug = matches.is_present("${DEBUG_FLAG}");
3940
match Engine::new(matches) {

0 commit comments

Comments
 (0)