Skip to content

Commit

Permalink
feat(CLI): repeated required args
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Byron committed Apr 22, 2015
1 parent a4b73cc commit c14ef9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/mako/cli/docs/commands.md.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/mako/cli/lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/mako/cli/lib/docopt.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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)">\
<%
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<%
Expand Down

0 comments on commit c14ef9a

Please sign in to comment.