From c14ef9afc86a17b5bc3952882f98fc7bf7a2ced8 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 22 Apr 2015 10:28:13 +0200 Subject: [PATCH] feat(CLI): repeated required args * Seem to work for docopt, mkdocs and code itself * mkdocs now show type of required params * some code which deals with converting elements to their target types is totally untested right now. Related to #77 --- src/mako/cli/docs/commands.md.mako | 7 +++++-- src/mako/cli/lib/cli.py | 5 +++++ src/mako/cli/lib/docopt.mako | 4 ++-- src/mako/cli/lib/engine.mako | 9 ++++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/mako/cli/docs/commands.md.mako b/src/mako/cli/docs/commands.md.mako index c2a493473f..225694dc97 100644 --- a/src/mako/cli/docs/commands.md.mako +++ b/src/mako/cli/docs/commands.md.mako @@ -5,7 +5,7 @@ from cli import (subcommand_md_filename, new_method_context, SPLIT_START, SPLIT_END, pretty, SCOPE_FLAG, mangle_subcommand, is_request_value_property, FIELD_SEP, PARAM_FLAG, UPLOAD_FLAG, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG, OUTPUT_FLAG, to_cli_schema, cli_schema_to_yaml, SchemaEntry, - STRUCT_FLAG, field_to_value, CTYPE_ARRAY, CTYPE_MAP) + STRUCT_FLAG, field_to_value, CTYPE_ARRAY, CTYPE_MAP, to_docopt_arg) from copy import deepcopy @@ -51,8 +51,11 @@ You can set the scope for this method like this: `${util.program_name()} --${SCO % if rprops: # Required Scalar ${len(rprops) > 1 and 'Arguments' or 'Argument'} % for p in rprops: -* **<${mangle_subcommand(p.name)}\>** +* **${to_docopt_arg(p) | xml_escape}** *(${p.type})* - ${p.get('description') or NO_DESC | xml_escape, indent_all_but_first_by(2)} +% if p.get('repeated'): + - This property can be specified one or more times +% endif % endfor # each required property (which is not the request value) % endif # have required properties % if mc.request_value: diff --git a/src/mako/cli/lib/cli.py b/src/mako/cli/lib/cli.py index 9cbe970c12..8e51c23173 100644 --- a/src/mako/cli/lib/cli.py +++ b/src/mako/cli/lib/cli.py @@ -121,6 +121,11 @@ def actual_json_type(name, type): return 'int64' return type +# return a string representing property `p` suitable for docopt argument parsing +def to_docopt_arg(p): + return '<%s>%s' % (mangle_subcommand(p.name), p.get('repeated', False) and '...' or '') + + # Return schema' with fields dict: { 'field1' : SchemaField(...), 'SubSchema': schema' } def to_cli_schema(c, schema): res = deepcopy(schema) diff --git a/src/mako/cli/lib/docopt.mako b/src/mako/cli/lib/docopt.mako index 5264a03403..4cb0685224 100644 --- a/src/mako/cli/lib/docopt.mako +++ b/src/mako/cli/lib/docopt.mako @@ -3,7 +3,7 @@ from util import (put_and, supports_scopes) from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG, CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG, - CONFIG_DIR_FLAG, KEY_VALUE_ARG) + CONFIG_DIR_FLAG, KEY_VALUE_ARG, to_docopt_arg) %>\ <%def name="new(c)">\ <% @@ -23,7 +23,7 @@ Usage: for p in mc.required_props: if is_request_value_property(mc, p): continue - args.append('<%s>' % mangle_subcommand(p.name)) + args.append(to_docopt_arg(p)) # end for each required property if mc.request_value: diff --git a/src/mako/cli/lib/engine.mako b/src/mako/cli/lib/engine.mako index 5958dac3b1..6fb4ffd38a 100644 --- a/src/mako/cli/lib/engine.mako +++ b/src/mako/cli/lib/engine.mako @@ -15,7 +15,7 @@ def borrow_prefix(p): ptype = p.get('type', None) borrow = '' - if ptype not in POD_TYPES or ptype in ('string', None): + if ptype not in POD_TYPES or ptype in ('string', None) or p.get('repeated', False): borrow = '&' return borrow @@ -175,7 +175,14 @@ if opt.flag_${flag_name} { <% request_prop_type = prop_type %>\ let mut ${prop_name} = api::${prop_type}::default(); % elif p.type != 'string': + % if p.get('repeated', False): +let ${prop_name}: Vec<${prop_type} = Vec::new(); +for (arg_id, arg) in ${opt_ident}.iter().enumerate() { + ${prop_name}.push(arg_from_str(&arg, err, "<${mangle_subcommand(p.name)}>", arg_id), "${p.type}")); +} + % else: let ${prop_name}: ${prop_type} = arg_from_str(&${opt_ident}, err, "<${mangle_subcommand(p.name)}>", "${p.type}"); + % endif # handle repeated values % endif # handle request value % endfor # each required parameter <%