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
Provenance
- Commit
84fbcb2- Tree
779b734- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
24efee0
3 files changed+78 added−10 removed
modified:docs/options.rst+19 added
72 unmodified lines73 $ python commit.py -m foo -m bar74 foo75 bar76 77+Counting78+--------79+80+If you have used ``optparse`` or ``argparse`` before you might be missing81+support for counting. This is a very rarely useful feature, usually only82+useful for implementing verbosity flags. It can however be emulated by83+using the multiple flag and taking the length of the end result::84+85+ @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)90+91+And on the command line::92+93+ $ python log.py -vvv94+ Verbosity: 395+96 Boolean Flags97 -------------98 99 Boolean flags are options that can be enabled or disabled. This can be273 unmodified linesmodified:docs/why.rst+52 added-1 removed
40 unmodified lines41 Click was written to support the `Flask <http://flask.pocoo.org/>`_42 microframework ecosystem because no tool could provide it with the43 functionality it needed.44 45-To get an understanding of what Click is all about I strongly recommend45+To get an understanding of what click is all about I strongly recommend46 looking at the :ref:`complex-guide` chapter to see what it's useful for.47+48+Why not Argparse?49+-----------------50+51+Click is internally based on optparse instead of argparse. This however52+is an implementation detail that a user does not have to concern himself53+with. The reason however click is not using argparse is that it has some54+problematic behaviors that make handling arbitrary command line interfaces55+hard:56+57+* 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.66+67+Why not Docopt etc.?68+--------------------69+70+Docopt and many tools like it are cool in how they work but very few of71+these tools deal with nesting of commands. To the best of the developer's72+knowledge, click is the first Python library that tries to aim for73+composability of applications that goes beyond what the system itself74+supports.75+76+For instance a Click subcommand could accept all the arguments unparsed77+and then be parsed with docopt if that command would want to work that78+way.79+80+Why Hardcoded Behaviors?81+------------------------82+83+The other question is why click goes away from optparse and hardcodes84+certain behaviors instead of staying configurable. There are multiple85+reasons for this. The biggest one is that too much configurability makes86+it hard to achieve a consistent command line experience.87+88+The best example for this is optparse's ``callback`` functionality for89+accepting arbitrary number of arguments. Due to syntactical ambuiguities90+on the command line there is no way to implement fully variadic arguments.91+There are always tradeoffs that need to be made and in case of92+``argparse`` these tradeoffs have been critical enough, that a system like93+click cannot even be implemented on top of it.94+95+In this particular case click attempts to stay with a handful of accepted96+paradigms for building command line interfaces that can be well documented97+and tested.modified:click.py+7 added-9 removed
1013 unmodified lines1014 """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))1020 1021 def full_process_value(self, ctx, value):1022 value = self.process_value(ctx, value)111 unmodified lines1134 self.allow_from_autoenv = allow_from_autoenv1135 1136 # 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 lines1192 'nargs': self.nargs,1193 'metavar': self.make_metavar(),1194 }1195 1196+ action = self.multiple and 'append' or 'store'1197+1198 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)1219 1220 def get_default(self, ctx):1221 # If we're a non boolean flag out default is more complex because405 unmodified lines