Skip to content

Commit c14ef9a

Browse files
committed
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
1 parent a4b73cc commit c14ef9a

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/mako/cli/docs/commands.md.mako

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from cli import (subcommand_md_filename, new_method_context, SPLIT_START, SPLIT_END, pretty, SCOPE_FLAG,
66
mangle_subcommand, is_request_value_property, FIELD_SEP, PARAM_FLAG, UPLOAD_FLAG, docopt_mode,
77
FILE_ARG, MIME_ARG, OUT_ARG, OUTPUT_FLAG, to_cli_schema, cli_schema_to_yaml, SchemaEntry,
8-
STRUCT_FLAG, field_to_value, CTYPE_ARRAY, CTYPE_MAP)
8+
STRUCT_FLAG, field_to_value, CTYPE_ARRAY, CTYPE_MAP, to_docopt_arg)
99
1010
from copy import deepcopy
1111
@@ -51,8 +51,11 @@ You can set the scope for this method like this: `${util.program_name()} --${SCO
5151
% if rprops:
5252
# Required Scalar ${len(rprops) > 1 and 'Arguments' or 'Argument'}
5353
% for p in rprops:
54-
* **<${mangle_subcommand(p.name)}\>**
54+
* **${to_docopt_arg(p) | xml_escape}** *(${p.type})*
5555
- ${p.get('description') or NO_DESC | xml_escape, indent_all_but_first_by(2)}
56+
% if p.get('repeated'):
57+
- This property can be specified one or more times
58+
% endif
5659
% endfor # each required property (which is not the request value)
5760
% endif # have required properties
5861
% if mc.request_value:

src/mako/cli/lib/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ def actual_json_type(name, type):
121121
return 'int64'
122122
return type
123123

124+
# return a string representing property `p` suitable for docopt argument parsing
125+
def to_docopt_arg(p):
126+
return '<%s>%s' % (mangle_subcommand(p.name), p.get('repeated', False) and '...' or '')
127+
128+
124129
# Return schema' with fields dict: { 'field1' : SchemaField(...), 'SubSchema': schema' }
125130
def to_cli_schema(c, schema):
126131
res = deepcopy(schema)

src/mako/cli/lib/docopt.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from util import (put_and, supports_scopes)
44
from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG,
55
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
6-
CONFIG_DIR_FLAG, KEY_VALUE_ARG)
6+
CONFIG_DIR_FLAG, KEY_VALUE_ARG, to_docopt_arg)
77
%>\
88
<%def name="new(c)">\
99
<%
@@ -23,7 +23,7 @@ Usage:
2323
for p in mc.required_props:
2424
if is_request_value_property(mc, p):
2525
continue
26-
args.append('<%s>' % mangle_subcommand(p.name))
26+
args.append(to_docopt_arg(p))
2727
# end for each required property
2828
2929
if mc.request_value:

src/mako/cli/lib/engine.mako

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def borrow_prefix(p):
1616
ptype = p.get('type', None)
1717
borrow = ''
18-
if ptype not in POD_TYPES or ptype in ('string', None):
18+
if ptype not in POD_TYPES or ptype in ('string', None) or p.get('repeated', False):
1919
borrow = '&'
2020
return borrow
2121
@@ -175,7 +175,14 @@ if opt.flag_${flag_name} {
175175
<% request_prop_type = prop_type %>\
176176
let mut ${prop_name} = api::${prop_type}::default();
177177
% elif p.type != 'string':
178+
% if p.get('repeated', False):
179+
let ${prop_name}: Vec<${prop_type} = Vec::new();
180+
for (arg_id, arg) in ${opt_ident}.iter().enumerate() {
181+
${prop_name}.push(arg_from_str(&arg, err, "<${mangle_subcommand(p.name)}>", arg_id), "${p.type}"));
182+
}
183+
% else:
178184
let ${prop_name}: ${prop_type} = arg_from_str(&${opt_ident}, err, "<${mangle_subcommand(p.name)}>", "${p.type}");
185+
% endif # handle repeated values
179186
% endif # handle request value
180187
% endfor # each required parameter
181188
<%

0 commit comments

Comments
 (0)