|
3 | 3 | Utilities for the CLI functions.
|
4 | 4 | """
|
5 | 5 | from __future__ import print_function, division, unicode_literals, absolute_import
|
6 |
| -import re |
7 | 6 |
|
| 7 | +from builtins import bytes, str |
| 8 | + |
| 9 | +import re |
8 | 10 | import click
|
| 11 | +import json |
9 | 12 |
|
10 | 13 | from .instance import import_module
|
11 | 14 | from ..interfaces.base import InputMultiPath, traits
|
|
16 | 19 | UNKNOWN_OPTIONS = dict(allow_extra_args=True,
|
17 | 20 | ignore_unknown_options=True)
|
18 | 21 |
|
19 |
| - |
20 | 22 | # specification of existing ParamTypes
|
21 | 23 | ExistingDirPath = click.Path(exists=True, file_okay=False, resolve_path=True)
|
22 | 24 | ExistingFilePath = click.Path(exists=True, dir_okay=False, resolve_path=True)
|
@@ -63,18 +65,62 @@ def add_args_options(arg_parser, interface):
|
63 | 65 | # Escape any % signs with a %
|
64 | 66 | desc = desc.replace('%', '%%')
|
65 | 67 | args = {}
|
| 68 | + has_multiple_inner_traits = False |
66 | 69 |
|
67 | 70 | if spec.is_trait_type(traits.Bool):
|
68 | 71 | args["default"] = getattr(inputs, name)
|
69 | 72 | args["action"] = 'store_true'
|
70 | 73 |
|
71 |
| - if hasattr(spec, "mandatory") and spec.mandatory: |
| 74 | + print(name, spec.trait_type) |
| 75 | + # current support is for simple trait types |
| 76 | + if not spec.inner_traits: |
| 77 | + if not spec.is_trait_type(traits.TraitCompound): |
| 78 | + trait_type = type(spec.trait_type.default_value) |
| 79 | + if trait_type in (bytes, str, int, float): |
| 80 | + if trait_type == bytes: |
| 81 | + trait_type = str |
| 82 | + args["type"] = trait_type |
| 83 | + elif len(spec.inner_traits) == 1: |
| 84 | + trait_type = type(spec.inner_traits[0].trait_type.default_value) |
| 85 | + if trait_type == bytes: |
| 86 | + trait_type = str |
| 87 | + if trait_type in (bytes, bool, str, int, float): |
| 88 | + args["type"] = trait_type |
| 89 | + else: |
| 90 | + if len(spec.inner_traits) > 1: |
| 91 | + if not spec.is_trait_type(traits.Dict): |
| 92 | + has_multiple_inner_traits = True |
| 93 | + |
| 94 | + if getattr(spec, "mandatory", False): |
72 | 95 | if spec.is_trait_type(InputMultiPath):
|
73 | 96 | args["nargs"] = "+"
|
| 97 | + elif spec.is_trait_type(traits.List): |
| 98 | + if (spec.trait_type.minlen == spec.trait_type.maxlen) and \ |
| 99 | + spec.trait_type.maxlen: |
| 100 | + args["nargs"] = spec.trait_type.maxlen |
| 101 | + else: |
| 102 | + args["nargs"] = "+" |
| 103 | + elif spec.is_trait_type(traits.Dict): |
| 104 | + args["type"] = json.loads |
| 105 | + |
| 106 | + if has_multiple_inner_traits: |
| 107 | + raise NotImplementedError( |
| 108 | + ('This interface cannot be used. via the' |
| 109 | + ' command line as multiple inner traits' |
| 110 | + ' are currently not supported for mandatory' |
| 111 | + ' argument: {}.'.format(name))) |
74 | 112 | arg_parser.add_argument(name, help=desc, **args)
|
75 | 113 | else:
|
76 | 114 | if spec.is_trait_type(InputMultiPath):
|
77 | 115 | args["nargs"] = "*"
|
78 |
| - arg_parser.add_argument("--%s" % name, dest=name, |
79 |
| - help=desc, **args) |
| 116 | + elif spec.is_trait_type(traits.List): |
| 117 | + if (spec.trait_type.minlen == spec.trait_type.maxlen) and \ |
| 118 | + spec.trait_type.maxlen: |
| 119 | + args["nargs"] = spec.trait_type.maxlen |
| 120 | + else: |
| 121 | + args["nargs"] = "*" |
| 122 | + if not has_multiple_inner_traits: |
| 123 | + arg_parser.add_argument("--%s" % name, dest=name, |
| 124 | + help=desc, **args) |
| 125 | + |
80 | 126 | return arg_parser
|
0 commit comments