Improved handling of dos/windows arguments greatly.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d428e69 · Browse files · 5 files·+85 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+85 added−23 removed
- tests:+29 added−0 removed
- other:+56 added−23 removed
Provenance
- Commit
133c417- Tree
e4bb431- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d428e69
5 files changed+85 added−23 removed
modified:click/core.py+17 added-7 removed
827 unmodified lines828 def __init__(self, param_decls=None, type=None, required=False,829 default=None, callback=None, nargs=1, metavar=None,830 expose_value=True, is_eager=False, envvar=None):831 self.name, self.opts, self.secondary_opts = \832- self._parse_decls(param_decls or ())832+ self._parse_decls(param_decls or (), expose_value)833 self.type = convert_type(type, default)834 self.required = required835 self.callback = callback836 self.nargs = nargs215 unmodified lines1052 elif self.is_flag:1053 raise TypeError('Options cannot be count and flags at '1054 'the same time.')1055 1056- def _parse_decls(self, decls):1056+ def _parse_decls(self, decls, expose_value):1057 opts = []1058 secondary_opts = []1059 name = None1060 possible_names = []3 unmodified lines1064 if name is not None:1065 raise TypeError('Name defined twice')1066 name = decl1067 else:1068+ split_char = decl[:1] == '/' and ';' or '/'1068- if '/' in decl:1069- first, second = decl.split('/', 1)1069+ if split_char in decl:1070+ first, second = decl.split(split_char, 1)1071+ first = first.rstrip()1072 possible_names.append(split_opt(first))1073 opts.append(first)1072- secondary_opts.append(second)1074+ secondary_opts.append(second.lstrip())1075 else:1076 possible_names.append(split_opt(decl))1077 opts.append(decl)1078 3 unmodified lines1082 if not isidentifier(name):1083 name = None1084 1085 if name is None:1086+ if not expose_value:1087+ return None, opts, secondary_opts1088 raise TypeError('Could not determine name for option')1089 1090 return name, opts, secondary_opts1091 26 unmodified lines1118 kwargs['action'] = action1119 parser.add_option(self.opts, **kwargs)1120 1121 def get_help_record(self, ctx):1122+ any_prefix_is_slash = []1123+1124 def _write_opts(opts):1125 rv = []1126 for opt in opts:1127 prefix = split_opt(opt)[0]1128+ if prefix == '/':1129+ any_prefix_is_slash[:] = [True]1130 rv.append((len(prefix), opt))1131 1132 rv.sort(key=lambda x: x[0])1133 14 unmodified lines1148 extra.append('required')1149 if extra:1150 help = '%s[%s]' % (help and help + ' ' or '', '; '.join(extra))1151 1144- return (' / '.join(rv), help)1152+ return ((any_prefix_is_slash and '; ' or ' / ').join(rv), help)1153 1154 def get_default(self, ctx):1155 # If we're a non boolean flag out default is more complex because1156 # we need to look at all flags in the same group to figure out78 unmodified lines1235 if self.nargs != 1:1236 var += '...'1237 return var1238 1231- def _parse_decls(self, decls):1239+ def _parse_decls(self, decls, expose_value):1240 if not decls:1241+ if not expose_value:1242+ return None, [], []1243 raise TypeError('Could not determine name for argument')1244 if len(decls) == 1:1245 name = arg = decls[0]1246 name = name.replace('-', '_').lower()17 unmodified linesmodified:click/parser.py+10 added-16 removed
53 unmodified lines54 self._long_opts = []55 self.prefixes = set()56 57 for opt in opts:58- prefix = split_opt(opt)[0]58+ prefix, value = split_opt(opt)59 if not prefix:60 raise ValueError('Invalid start character for option (%s)'61 % opt)62 self.prefixes.add(prefix[0])63- if len(prefix) == 1:63+ if len(prefix) == 1 and len(value) == 1:64 self._short_opts.append(opt)65 else:66 self._long_opts.append(opt)67 self.prefixes.add(prefix)133 unmodified lines201 state.rargs = []202 203 def _process_args_for_options(self, state):204 while state.rargs:205- arg = state.rargs[0]205+ arg = state.rargs.pop(0)206 arglen = len(arg)207 # Double dash es always handled explicitly regardless of what208 # prefixes are valid.209 if arg == '--':210- del state.rargs[0]210 return212- elif arg[:2] in self._opt_prefixes and arglen > 2:213- # process a single long option (possibly with value(s))214- self._process_long_opt(state)211 elif arg[:1] in self._opt_prefixes and arglen > 1:216- # process a cluster of short options (possibly with217- # value(s) for the last one only)218- self._process_short_opts(state)212+ self._process_opts(arg, state)213 elif self.allow_interspersed_args:214 state.largs.append(arg)221- del state.rargs[0]215 else:216+ state.rargs.insert(0, arg)217 return218 219 # Say this is the original argument list:220 # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]36 unmodified lines257 possibilities.sort()258 self._error('no such option: %s. (Possible options: %s)'259 % (opt, ', '.join(possibilities)))260 267- def _process_long_opt(self, state):268- arg = state.rargs.pop(0)269-261+ def _process_long_opt(self, arg, state):262 # Value explicitly attached to arg? Pretend it's the next263 # argument.264 if '=' in arg:265 opt, next_arg = arg.split('=', 1)25 unmodified lines291 value = None292 293 option.process(value, state)294 303- def _process_short_opts(self, state):304- arg = state.rargs.pop(0)295+ def _process_opts(self, arg, state):296+ if '=' in arg or arg in self._long_opt:297+ return self._process_long_opt(arg, state)298+299 stop = False300 i = 1301 prefix = arg[0]302 for ch in arg[1:]:36 unmodified linesmodified:docs/options.rst+27 added
146 unmodified lines147 .. click:run::148 149 invoke(info, args=['--shout'])150 151+Note that if a slash is contained in your option already (for instance if152+you use windows style parameters where ``/`` is the prefix character you153+can alternatively split the parameters through ``;`` instead):154+155+.. click:example::156+157+ @click.command()158+ @click.option('/debug;/no-debug')159+ def log(debug):160+ click.echo('debug=%s' % debug)161+162+ if __name__ == '__main__':163+ log()164+165 Feature Switches166 ----------------167 168 In addition to boolean flags, there are also feature switches. These are344 unmodified lines513 514 invoke(chmod, args=['+w'])515 invoke(chmod, args=['-w'])516 517+Note that if you are using ``/`` as prefix character and you want to use a518+boolean flag you need to separate it with ``;`` instead of ``/``:519+520+.. click:example::521+522+ @click.command()523+ @click.option('/debug;/no-debug')524+ def log(debug):525+ click.echo('debug=%s' % debug)526+527+ if __name__ == '__main__':528+ log()529+530 .. _ranges:531 532 Range Options533 -------------75 unmodified linesmodified:tests/test_options.py+29 added
125 unmodified lines126 in result.output127 128 result = runner.invoke(cmd, ['--foo', '42'])129 assert result.output == '42\n'130+131+132+def test_winstyle_options(runner):133+ @click.command(add_help_option=False)134+ @click.option('/debug;/no-debug', help='Enables or disables debug mode.')135+ @click.help_option('/?')136+ def cmd(debug):137+ click.echo(debug)138+139+ result = runner.invoke(cmd, ['/debug'])140+ assert result.output == 'True\n'141+ result = runner.invoke(cmd, ['/no-debug'])142+ assert result.output == 'False\n'143+ result = runner.invoke(cmd, [])144+ assert result.output == 'False\n'145+ result = runner.invoke(cmd, ['/?'])146+ assert '/debug; /no-debug Enables or disables debug mode.' in result.output147+148+149+def test_legacy_options(runner):150+ @click.command()151+ @click.option('-whatever')152+ def cmd(whatever):153+ click.echo(whatever)154+155+ result = runner.invoke(cmd, ['-whatever', '42'])156+ assert result.output == '42\n'157+ result = runner.invoke(cmd, ['-whatever=23'])158+ assert result.output == '23\n'modified:CHANGES+2 added
32 unmodified lines33 - changed "yN" / "Yn" to "y/N" and "Y/n" in confirmation prompts.34 - added basic support for bash completion.35 - added :func:`getchar` to fetch a single character from the terminal.36 - errors now go to stderr as intended.37+- fixed various issues with more exotic parameter formats like DOS/Windows38+ style arguments.39 40 Version 1.141 -----------42 11 unmodified lines