Skip to content
Myr

Added some more statements on argparse and how verbosity can be implemented in click.

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 24efee0 · Browse files · 3 files+78 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+78 added−10 removed

  • other:+78 added−10 removed

3 files+78 added−10 removed

modified:docs/options.rst+19 added
72 unmodified lines
73 $ python commit.py -m foo -m bar74 foo75 bar7677Counting78--------7980If you have used ``optparse`` or ``argparse`` before you might be missing81support for counting. This is a very rarely useful feature, usually only82useful for implementing verbosity flags. It can however be emulated by83using the multiple flag and taking the length of the end result::8485 @click.command()86 @click.option('-v', '--verbose', is_flag=True, multiple=True)87 def log(verbose):88 verbosity = len(verbose)89 print('Verbosity: %s' % verbosity)9091And on the command line::9293 $ python log.py -vvv94 Verbosity: 39596Boolean Flags97-------------9899Boolean flags are options that can be enabled or disabled. This can be
273 unmodified lines
modified:docs/why.rst+52 added-1 removed
40 unmodified lines
41Click was written to support the `Flask <http://flask.pocoo.org/>`_42microframework ecosystem because no tool could provide it with the43functionality it needed.4445To get an understanding of what Click is all about I strongly recommend45To get an understanding of what click is all about I strongly recommend46looking at the :ref:`complex-guide` chapter to see what it's useful for.4748Why not Argparse?49-----------------5051Click is internally based on optparse instead of argparse. This however52is an implementation detail that a user does not have to concern himself53with. The reason however click is not using argparse is that it has some54problematic behaviors that make handling arbitrary command line interfaces55hard:5657* argparse has builtin magic behavior to guess if something is an58 argument or an option. This becomes a problem when dealing with59 incomplete command lines as it's not possible to know without having a60 full understanding of the command line how the parser is going to61 behave. This goes against click's ambitions of dispatching to62 subparsers.63* argparse currently does not support disabling of interspearsed64 arguments. Without this feature it's not possible to safely implement65 click's nested parsing nature.6667Why not Docopt etc.?68--------------------6970Docopt and many tools like it are cool in how they work but very few of71these tools deal with nesting of commands. To the best of the developer's72knowledge, click is the first Python library that tries to aim for73composability of applications that goes beyond what the system itself74supports.7576For instance a Click subcommand could accept all the arguments unparsed77and then be parsed with docopt if that command would want to work that78way.7980Why Hardcoded Behaviors?81------------------------8283The other question is why click goes away from optparse and hardcodes84certain behaviors instead of staying configurable. There are multiple85reasons for this. The biggest one is that too much configurability makes86it hard to achieve a consistent command line experience.8788The best example for this is optparse's ``callback`` functionality for89accepting arbitrary number of arguments. Due to syntactical ambuiguities90on the command line there is no way to implement fully variadic arguments.91There are always tradeoffs that need to be made and in case of92``argparse`` these tradeoffs have been critical enough, that a system like93click cannot even be implemented on top of it.9495In this particular case click attempts to stay with a handful of accepted96paradigms for building command line interfaces that can be well documented97and tested.
modified:click.py+7 added-9 removed
1013 unmodified lines
1014 """1015 def _convert(value, level):1016 if level == 0:1017 return self.type(value, self, ctx)1018 return tuple(_convert(x, level - 1) for x in value)1018 return tuple(_convert(x, level - 1) for x in value or ())1019 return _convert(value, (self.nargs != 1) + bool(self.multiple))10201021 def full_process_value(self, ctx, value):1022 value = self.process_value(ctx, value)
111 unmodified lines
1134 self.allow_from_autoenv = allow_from_autoenv11351136 # Sanity check for stuff we don't support1137 if __debug__:1138 if self.multiple and self.is_bool_flag:1139 raise TypeError('Boolean flags and multiple values are not '1140 'compatible.')1138 if self.prompt and self.is_flag and not self.is_bool_flag:1139 raise TypeError('Cannot prompt for flags that are not bools.')1140 if not self.is_bool_flag and self.secondary_opts:1141 raise TypeError('Got secondary option for non boolean flag.')
50 unmodified lines
1192 'nargs': self.nargs,1193 'metavar': self.make_metavar(),1194 }11951196 action = self.multiple and 'append' or 'store'11971198 if self.is_flag:1199 kwargs.pop('nargs', None)1200 if self.is_bool_flag and self.secondary_opts:1202 pos_opt = optparse.Option(*self.opts, action='store_true',1201 pos_opt = optparse.Option(*self.opts, action=action + '_true',1202 **kwargs)1203 kwargs.pop('default', None)1204 kwargs.pop('help', None)1205 neg_opt = optparse.Option(*self.secondary_opts,1207 action='store_false',1206 action=action + '_false',1207 help=optparse.SUPPRESS_HELP,1208 **kwargs)1209 pos_opt._negative_version = neg_opt1210 parser.add_option(pos_opt)1211 parser.add_option(neg_opt)1212 else:1214 action = self.multiple and 'append_const' or 'store_const'1215 parser.add_option(*self.opts, action=action,1213 parser.add_option(*self.opts, action=action + '_const',1214 const=self.flag_value,1215 **kwargs)1216 else:1219 kwargs['action'] = self.multiple and 'append' or 'store'1217 kwargs['action'] = action1218 parser.add_option(*self.opts, **kwargs)12191220 def get_default(self, ctx):1221 # If we're a non boolean flag out default is more complex because
405 unmodified lines