core.py 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. import errno
  2. import inspect
  3. import os
  4. import sys
  5. from contextlib import contextmanager
  6. from itertools import repeat
  7. from functools import update_wrapper
  8. from .types import convert_type, IntRange, BOOL
  9. from .utils import PacifyFlushWrapper, make_str, make_default_short_help, \
  10. echo, get_os_args
  11. from .exceptions import ClickException, UsageError, BadParameter, Abort, \
  12. MissingParameter, Exit
  13. from .termui import prompt, confirm, style
  14. from .formatting import HelpFormatter, join_options
  15. from .parser import OptionParser, split_opt
  16. from .globals import push_context, pop_context
  17. from ._compat import PY2, isidentifier, iteritems, string_types
  18. from ._unicodefun import _check_for_unicode_literals, _verify_python3_env
  19. _missing = object()
  20. SUBCOMMAND_METAVAR = 'COMMAND [ARGS]...'
  21. SUBCOMMANDS_METAVAR = 'COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...'
  22. DEPRECATED_HELP_NOTICE = ' (DEPRECATED)'
  23. DEPRECATED_INVOKE_NOTICE = 'DeprecationWarning: ' + \
  24. 'The command %(name)s is deprecated.'
  25. def _maybe_show_deprecated_notice(cmd):
  26. if cmd.deprecated:
  27. echo(style(DEPRECATED_INVOKE_NOTICE % {'name': cmd.name}, fg='red'), err=True)
  28. def fast_exit(code):
  29. """Exit without garbage collection, this speeds up exit by about 10ms for
  30. things like bash completion.
  31. """
  32. sys.stdout.flush()
  33. sys.stderr.flush()
  34. os._exit(code)
  35. def _bashcomplete(cmd, prog_name, complete_var=None):
  36. """Internal handler for the bash completion support."""
  37. if complete_var is None:
  38. complete_var = '_%s_COMPLETE' % (prog_name.replace('-', '_')).upper()
  39. complete_instr = os.environ.get(complete_var)
  40. if not complete_instr:
  41. return
  42. from ._bashcomplete import bashcomplete
  43. if bashcomplete(cmd, prog_name, complete_var, complete_instr):
  44. fast_exit(1)
  45. def _check_multicommand(base_command, cmd_name, cmd, register=False):
  46. if not base_command.chain or not isinstance(cmd, MultiCommand):
  47. return
  48. if register:
  49. hint = 'It is not possible to add multi commands as children to ' \
  50. 'another multi command that is in chain mode'
  51. else:
  52. hint = 'Found a multi command as subcommand to a multi command ' \
  53. 'that is in chain mode. This is not supported'
  54. raise RuntimeError('%s. Command "%s" is set to chain and "%s" was '
  55. 'added as subcommand but it in itself is a '
  56. 'multi command. ("%s" is a %s within a chained '
  57. '%s named "%s").' % (
  58. hint, base_command.name, cmd_name,
  59. cmd_name, cmd.__class__.__name__,
  60. base_command.__class__.__name__,
  61. base_command.name))
  62. def batch(iterable, batch_size):
  63. return list(zip(*repeat(iter(iterable), batch_size)))
  64. def invoke_param_callback(callback, ctx, param, value):
  65. code = getattr(callback, '__code__', None)
  66. args = getattr(code, 'co_argcount', 3)
  67. if args < 3:
  68. # This will become a warning in Click 3.0:
  69. from warnings import warn
  70. warn(Warning('Invoked legacy parameter callback "%s". The new '
  71. 'signature for such callbacks starting with '
  72. 'click 2.0 is (ctx, param, value).'
  73. % callback), stacklevel=3)
  74. return callback(ctx, value)
  75. return callback(ctx, param, value)
  76. @contextmanager
  77. def augment_usage_errors(ctx, param=None):
  78. """Context manager that attaches extra information to exceptions that
  79. fly.
  80. """
  81. try:
  82. yield
  83. except BadParameter as e:
  84. if e.ctx is None:
  85. e.ctx = ctx
  86. if param is not None and e.param is None:
  87. e.param = param
  88. raise
  89. except UsageError as e:
  90. if e.ctx is None:
  91. e.ctx = ctx
  92. raise
  93. def iter_params_for_processing(invocation_order, declaration_order):
  94. """Given a sequence of parameters in the order as should be considered
  95. for processing and an iterable of parameters that exist, this returns
  96. a list in the correct order as they should be processed.
  97. """
  98. def sort_key(item):
  99. try:
  100. idx = invocation_order.index(item)
  101. except ValueError:
  102. idx = float('inf')
  103. return (not item.is_eager, idx)
  104. return sorted(declaration_order, key=sort_key)
  105. class Context(object):
  106. """The context is a special internal object that holds state relevant
  107. for the script execution at every single level. It's normally invisible
  108. to commands unless they opt-in to getting access to it.
  109. The context is useful as it can pass internal objects around and can
  110. control special execution features such as reading data from
  111. environment variables.
  112. A context can be used as context manager in which case it will call
  113. :meth:`close` on teardown.
  114. .. versionadded:: 2.0
  115. Added the `resilient_parsing`, `help_option_names`,
  116. `token_normalize_func` parameters.
  117. .. versionadded:: 3.0
  118. Added the `allow_extra_args` and `allow_interspersed_args`
  119. parameters.
  120. .. versionadded:: 4.0
  121. Added the `color`, `ignore_unknown_options`, and
  122. `max_content_width` parameters.
  123. :param command: the command class for this context.
  124. :param parent: the parent context.
  125. :param info_name: the info name for this invocation. Generally this
  126. is the most descriptive name for the script or
  127. command. For the toplevel script it is usually
  128. the name of the script, for commands below it it's
  129. the name of the script.
  130. :param obj: an arbitrary object of user data.
  131. :param auto_envvar_prefix: the prefix to use for automatic environment
  132. variables. If this is `None` then reading
  133. from environment variables is disabled. This
  134. does not affect manually set environment
  135. variables which are always read.
  136. :param default_map: a dictionary (like object) with default values
  137. for parameters.
  138. :param terminal_width: the width of the terminal. The default is
  139. inherit from parent context. If no context
  140. defines the terminal width then auto
  141. detection will be applied.
  142. :param max_content_width: the maximum width for content rendered by
  143. Click (this currently only affects help
  144. pages). This defaults to 80 characters if
  145. not overridden. In other words: even if the
  146. terminal is larger than that, Click will not
  147. format things wider than 80 characters by
  148. default. In addition to that, formatters might
  149. add some safety mapping on the right.
  150. :param resilient_parsing: if this flag is enabled then Click will
  151. parse without any interactivity or callback
  152. invocation. Default values will also be
  153. ignored. This is useful for implementing
  154. things such as completion support.
  155. :param allow_extra_args: if this is set to `True` then extra arguments
  156. at the end will not raise an error and will be
  157. kept on the context. The default is to inherit
  158. from the command.
  159. :param allow_interspersed_args: if this is set to `False` then options
  160. and arguments cannot be mixed. The
  161. default is to inherit from the command.
  162. :param ignore_unknown_options: instructs click to ignore options it does
  163. not know and keeps them for later
  164. processing.
  165. :param help_option_names: optionally a list of strings that define how
  166. the default help parameter is named. The
  167. default is ``['--help']``.
  168. :param token_normalize_func: an optional function that is used to
  169. normalize tokens (options, choices,
  170. etc.). This for instance can be used to
  171. implement case insensitive behavior.
  172. :param color: controls if the terminal supports ANSI colors or not. The
  173. default is autodetection. This is only needed if ANSI
  174. codes are used in texts that Click prints which is by
  175. default not the case. This for instance would affect
  176. help output.
  177. """
  178. def __init__(self, command, parent=None, info_name=None, obj=None,
  179. auto_envvar_prefix=None, default_map=None,
  180. terminal_width=None, max_content_width=None,
  181. resilient_parsing=False, allow_extra_args=None,
  182. allow_interspersed_args=None,
  183. ignore_unknown_options=None, help_option_names=None,
  184. token_normalize_func=None, color=None):
  185. #: the parent context or `None` if none exists.
  186. self.parent = parent
  187. #: the :class:`Command` for this context.
  188. self.command = command
  189. #: the descriptive information name
  190. self.info_name = info_name
  191. #: the parsed parameters except if the value is hidden in which
  192. #: case it's not remembered.
  193. self.params = {}
  194. #: the leftover arguments.
  195. self.args = []
  196. #: protected arguments. These are arguments that are prepended
  197. #: to `args` when certain parsing scenarios are encountered but
  198. #: must be never propagated to another arguments. This is used
  199. #: to implement nested parsing.
  200. self.protected_args = []
  201. if obj is None and parent is not None:
  202. obj = parent.obj
  203. #: the user object stored.
  204. self.obj = obj
  205. self._meta = getattr(parent, 'meta', {})
  206. #: A dictionary (-like object) with defaults for parameters.
  207. if default_map is None \
  208. and parent is not None \
  209. and parent.default_map is not None:
  210. default_map = parent.default_map.get(info_name)
  211. self.default_map = default_map
  212. #: This flag indicates if a subcommand is going to be executed. A
  213. #: group callback can use this information to figure out if it's
  214. #: being executed directly or because the execution flow passes
  215. #: onwards to a subcommand. By default it's None, but it can be
  216. #: the name of the subcommand to execute.
  217. #:
  218. #: If chaining is enabled this will be set to ``'*'`` in case
  219. #: any commands are executed. It is however not possible to
  220. #: figure out which ones. If you require this knowledge you
  221. #: should use a :func:`resultcallback`.
  222. self.invoked_subcommand = None
  223. if terminal_width is None and parent is not None:
  224. terminal_width = parent.terminal_width
  225. #: The width of the terminal (None is autodetection).
  226. self.terminal_width = terminal_width
  227. if max_content_width is None and parent is not None:
  228. max_content_width = parent.max_content_width
  229. #: The maximum width of formatted content (None implies a sensible
  230. #: default which is 80 for most things).
  231. self.max_content_width = max_content_width
  232. if allow_extra_args is None:
  233. allow_extra_args = command.allow_extra_args
  234. #: Indicates if the context allows extra args or if it should
  235. #: fail on parsing.
  236. #:
  237. #: .. versionadded:: 3.0
  238. self.allow_extra_args = allow_extra_args
  239. if allow_interspersed_args is None:
  240. allow_interspersed_args = command.allow_interspersed_args
  241. #: Indicates if the context allows mixing of arguments and
  242. #: options or not.
  243. #:
  244. #: .. versionadded:: 3.0
  245. self.allow_interspersed_args = allow_interspersed_args
  246. if ignore_unknown_options is None:
  247. ignore_unknown_options = command.ignore_unknown_options
  248. #: Instructs click to ignore options that a command does not
  249. #: understand and will store it on the context for later
  250. #: processing. This is primarily useful for situations where you
  251. #: want to call into external programs. Generally this pattern is
  252. #: strongly discouraged because it's not possibly to losslessly
  253. #: forward all arguments.
  254. #:
  255. #: .. versionadded:: 4.0
  256. self.ignore_unknown_options = ignore_unknown_options
  257. if help_option_names is None:
  258. if parent is not None:
  259. help_option_names = parent.help_option_names
  260. else:
  261. help_option_names = ['--help']
  262. #: The names for the help options.
  263. self.help_option_names = help_option_names
  264. if token_normalize_func is None and parent is not None:
  265. token_normalize_func = parent.token_normalize_func
  266. #: An optional normalization function for tokens. This is
  267. #: options, choices, commands etc.
  268. self.token_normalize_func = token_normalize_func
  269. #: Indicates if resilient parsing is enabled. In that case Click
  270. #: will do its best to not cause any failures and default values
  271. #: will be ignored. Useful for completion.
  272. self.resilient_parsing = resilient_parsing
  273. # If there is no envvar prefix yet, but the parent has one and
  274. # the command on this level has a name, we can expand the envvar
  275. # prefix automatically.
  276. if auto_envvar_prefix is None:
  277. if parent is not None \
  278. and parent.auto_envvar_prefix is not None and \
  279. self.info_name is not None:
  280. auto_envvar_prefix = '%s_%s' % (parent.auto_envvar_prefix,
  281. self.info_name.upper())
  282. else:
  283. auto_envvar_prefix = auto_envvar_prefix.upper()
  284. self.auto_envvar_prefix = auto_envvar_prefix
  285. if color is None and parent is not None:
  286. color = parent.color
  287. #: Controls if styling output is wanted or not.
  288. self.color = color
  289. self._close_callbacks = []
  290. self._depth = 0
  291. def __enter__(self):
  292. self._depth += 1
  293. push_context(self)
  294. return self
  295. def __exit__(self, exc_type, exc_value, tb):
  296. self._depth -= 1
  297. if self._depth == 0:
  298. self.close()
  299. pop_context()
  300. @contextmanager
  301. def scope(self, cleanup=True):
  302. """This helper method can be used with the context object to promote
  303. it to the current thread local (see :func:`get_current_context`).
  304. The default behavior of this is to invoke the cleanup functions which
  305. can be disabled by setting `cleanup` to `False`. The cleanup
  306. functions are typically used for things such as closing file handles.
  307. If the cleanup is intended the context object can also be directly
  308. used as a context manager.
  309. Example usage::
  310. with ctx.scope():
  311. assert get_current_context() is ctx
  312. This is equivalent::
  313. with ctx:
  314. assert get_current_context() is ctx
  315. .. versionadded:: 5.0
  316. :param cleanup: controls if the cleanup functions should be run or
  317. not. The default is to run these functions. In
  318. some situations the context only wants to be
  319. temporarily pushed in which case this can be disabled.
  320. Nested pushes automatically defer the cleanup.
  321. """
  322. if not cleanup:
  323. self._depth += 1
  324. try:
  325. with self as rv:
  326. yield rv
  327. finally:
  328. if not cleanup:
  329. self._depth -= 1
  330. @property
  331. def meta(self):
  332. """This is a dictionary which is shared with all the contexts
  333. that are nested. It exists so that click utilities can store some
  334. state here if they need to. It is however the responsibility of
  335. that code to manage this dictionary well.
  336. The keys are supposed to be unique dotted strings. For instance
  337. module paths are a good choice for it. What is stored in there is
  338. irrelevant for the operation of click. However what is important is
  339. that code that places data here adheres to the general semantics of
  340. the system.
  341. Example usage::
  342. LANG_KEY = __name__ + '.lang'
  343. def set_language(value):
  344. ctx = get_current_context()
  345. ctx.meta[LANG_KEY] = value
  346. def get_language():
  347. return get_current_context().meta.get(LANG_KEY, 'en_US')
  348. .. versionadded:: 5.0
  349. """
  350. return self._meta
  351. def make_formatter(self):
  352. """Creates the formatter for the help and usage output."""
  353. return HelpFormatter(width=self.terminal_width,
  354. max_width=self.max_content_width)
  355. def call_on_close(self, f):
  356. """This decorator remembers a function as callback that should be
  357. executed when the context tears down. This is most useful to bind
  358. resource handling to the script execution. For instance, file objects
  359. opened by the :class:`File` type will register their close callbacks
  360. here.
  361. :param f: the function to execute on teardown.
  362. """
  363. self._close_callbacks.append(f)
  364. return f
  365. def close(self):
  366. """Invokes all close callbacks."""
  367. for cb in self._close_callbacks:
  368. cb()
  369. self._close_callbacks = []
  370. @property
  371. def command_path(self):
  372. """The computed command path. This is used for the ``usage``
  373. information on the help page. It's automatically created by
  374. combining the info names of the chain of contexts to the root.
  375. """
  376. rv = ''
  377. if self.info_name is not None:
  378. rv = self.info_name
  379. if self.parent is not None:
  380. rv = self.parent.command_path + ' ' + rv
  381. return rv.lstrip()
  382. def find_root(self):
  383. """Finds the outermost context."""
  384. node = self
  385. while node.parent is not None:
  386. node = node.parent
  387. return node
  388. def find_object(self, object_type):
  389. """Finds the closest object of a given type."""
  390. node = self
  391. while node is not None:
  392. if isinstance(node.obj, object_type):
  393. return node.obj
  394. node = node.parent
  395. def ensure_object(self, object_type):
  396. """Like :meth:`find_object` but sets the innermost object to a
  397. new instance of `object_type` if it does not exist.
  398. """
  399. rv = self.find_object(object_type)
  400. if rv is None:
  401. self.obj = rv = object_type()
  402. return rv
  403. def lookup_default(self, name):
  404. """Looks up the default for a parameter name. This by default
  405. looks into the :attr:`default_map` if available.
  406. """
  407. if self.default_map is not None:
  408. rv = self.default_map.get(name)
  409. if callable(rv):
  410. rv = rv()
  411. return rv
  412. def fail(self, message):
  413. """Aborts the execution of the program with a specific error
  414. message.
  415. :param message: the error message to fail with.
  416. """
  417. raise UsageError(message, self)
  418. def abort(self):
  419. """Aborts the script."""
  420. raise Abort()
  421. def exit(self, code=0):
  422. """Exits the application with a given exit code."""
  423. raise Exit(code)
  424. def get_usage(self):
  425. """Helper method to get formatted usage string for the current
  426. context and command.
  427. """
  428. return self.command.get_usage(self)
  429. def get_help(self):
  430. """Helper method to get formatted help page for the current
  431. context and command.
  432. """
  433. return self.command.get_help(self)
  434. def invoke(*args, **kwargs):
  435. """Invokes a command callback in exactly the way it expects. There
  436. are two ways to invoke this method:
  437. 1. the first argument can be a callback and all other arguments and
  438. keyword arguments are forwarded directly to the function.
  439. 2. the first argument is a click command object. In that case all
  440. arguments are forwarded as well but proper click parameters
  441. (options and click arguments) must be keyword arguments and Click
  442. will fill in defaults.
  443. Note that before Click 3.2 keyword arguments were not properly filled
  444. in against the intention of this code and no context was created. For
  445. more information about this change and why it was done in a bugfix
  446. release see :ref:`upgrade-to-3.2`.
  447. """
  448. self, callback = args[:2]
  449. ctx = self
  450. # It's also possible to invoke another command which might or
  451. # might not have a callback. In that case we also fill
  452. # in defaults and make a new context for this command.
  453. if isinstance(callback, Command):
  454. other_cmd = callback
  455. callback = other_cmd.callback
  456. ctx = Context(other_cmd, info_name=other_cmd.name, parent=self)
  457. if callback is None:
  458. raise TypeError('The given command does not have a '
  459. 'callback that can be invoked.')
  460. for param in other_cmd.params:
  461. if param.name not in kwargs and param.expose_value:
  462. kwargs[param.name] = param.get_default(ctx)
  463. args = args[2:]
  464. with augment_usage_errors(self):
  465. with ctx:
  466. return callback(*args, **kwargs)
  467. def forward(*args, **kwargs):
  468. """Similar to :meth:`invoke` but fills in default keyword
  469. arguments from the current context if the other command expects
  470. it. This cannot invoke callbacks directly, only other commands.
  471. """
  472. self, cmd = args[:2]
  473. # It's also possible to invoke another command which might or
  474. # might not have a callback.
  475. if not isinstance(cmd, Command):
  476. raise TypeError('Callback is not a command.')
  477. for param in self.params:
  478. if param not in kwargs:
  479. kwargs[param] = self.params[param]
  480. return self.invoke(cmd, **kwargs)
  481. class BaseCommand(object):
  482. """The base command implements the minimal API contract of commands.
  483. Most code will never use this as it does not implement a lot of useful
  484. functionality but it can act as the direct subclass of alternative
  485. parsing methods that do not depend on the Click parser.
  486. For instance, this can be used to bridge Click and other systems like
  487. argparse or docopt.
  488. Because base commands do not implement a lot of the API that other
  489. parts of Click take for granted, they are not supported for all
  490. operations. For instance, they cannot be used with the decorators
  491. usually and they have no built-in callback system.
  492. .. versionchanged:: 2.0
  493. Added the `context_settings` parameter.
  494. :param name: the name of the command to use unless a group overrides it.
  495. :param context_settings: an optional dictionary with defaults that are
  496. passed to the context object.
  497. """
  498. #: the default for the :attr:`Context.allow_extra_args` flag.
  499. allow_extra_args = False
  500. #: the default for the :attr:`Context.allow_interspersed_args` flag.
  501. allow_interspersed_args = True
  502. #: the default for the :attr:`Context.ignore_unknown_options` flag.
  503. ignore_unknown_options = False
  504. def __init__(self, name, context_settings=None):
  505. #: the name the command thinks it has. Upon registering a command
  506. #: on a :class:`Group` the group will default the command name
  507. #: with this information. You should instead use the
  508. #: :class:`Context`\'s :attr:`~Context.info_name` attribute.
  509. self.name = name
  510. if context_settings is None:
  511. context_settings = {}
  512. #: an optional dictionary with defaults passed to the context.
  513. self.context_settings = context_settings
  514. def get_usage(self, ctx):
  515. raise NotImplementedError('Base commands cannot get usage')
  516. def get_help(self, ctx):
  517. raise NotImplementedError('Base commands cannot get help')
  518. def make_context(self, info_name, args, parent=None, **extra):
  519. """This function when given an info name and arguments will kick
  520. off the parsing and create a new :class:`Context`. It does not
  521. invoke the actual command callback though.
  522. :param info_name: the info name for this invokation. Generally this
  523. is the most descriptive name for the script or
  524. command. For the toplevel script it's usually
  525. the name of the script, for commands below it it's
  526. the name of the script.
  527. :param args: the arguments to parse as list of strings.
  528. :param parent: the parent context if available.
  529. :param extra: extra keyword arguments forwarded to the context
  530. constructor.
  531. """
  532. for key, value in iteritems(self.context_settings):
  533. if key not in extra:
  534. extra[key] = value
  535. ctx = Context(self, info_name=info_name, parent=parent, **extra)
  536. with ctx.scope(cleanup=False):
  537. self.parse_args(ctx, args)
  538. return ctx
  539. def parse_args(self, ctx, args):
  540. """Given a context and a list of arguments this creates the parser
  541. and parses the arguments, then modifies the context as necessary.
  542. This is automatically invoked by :meth:`make_context`.
  543. """
  544. raise NotImplementedError('Base commands do not know how to parse '
  545. 'arguments.')
  546. def invoke(self, ctx):
  547. """Given a context, this invokes the command. The default
  548. implementation is raising a not implemented error.
  549. """
  550. raise NotImplementedError('Base commands are not invokable by default')
  551. def main(self, args=None, prog_name=None, complete_var=None,
  552. standalone_mode=True, **extra):
  553. """This is the way to invoke a script with all the bells and
  554. whistles as a command line application. This will always terminate
  555. the application after a call. If this is not wanted, ``SystemExit``
  556. needs to be caught.
  557. This method is also available by directly calling the instance of
  558. a :class:`Command`.
  559. .. versionadded:: 3.0
  560. Added the `standalone_mode` flag to control the standalone mode.
  561. :param args: the arguments that should be used for parsing. If not
  562. provided, ``sys.argv[1:]`` is used.
  563. :param prog_name: the program name that should be used. By default
  564. the program name is constructed by taking the file
  565. name from ``sys.argv[0]``.
  566. :param complete_var: the environment variable that controls the
  567. bash completion support. The default is
  568. ``"_<prog_name>_COMPLETE"`` with prog_name in
  569. uppercase.
  570. :param standalone_mode: the default behavior is to invoke the script
  571. in standalone mode. Click will then
  572. handle exceptions and convert them into
  573. error messages and the function will never
  574. return but shut down the interpreter. If
  575. this is set to `False` they will be
  576. propagated to the caller and the return
  577. value of this function is the return value
  578. of :meth:`invoke`.
  579. :param extra: extra keyword arguments are forwarded to the context
  580. constructor. See :class:`Context` for more information.
  581. """
  582. # If we are in Python 3, we will verify that the environment is
  583. # sane at this point or reject further execution to avoid a
  584. # broken script.
  585. if not PY2:
  586. _verify_python3_env()
  587. else:
  588. _check_for_unicode_literals()
  589. if args is None:
  590. args = get_os_args()
  591. else:
  592. args = list(args)
  593. if prog_name is None:
  594. prog_name = make_str(os.path.basename(
  595. sys.argv and sys.argv[0] or __file__))
  596. # Hook for the Bash completion. This only activates if the Bash
  597. # completion is actually enabled, otherwise this is quite a fast
  598. # noop.
  599. _bashcomplete(self, prog_name, complete_var)
  600. try:
  601. try:
  602. with self.make_context(prog_name, args, **extra) as ctx:
  603. rv = self.invoke(ctx)
  604. if not standalone_mode:
  605. return rv
  606. # it's not safe to `ctx.exit(rv)` here!
  607. # note that `rv` may actually contain data like "1" which
  608. # has obvious effects
  609. # more subtle case: `rv=[None, None]` can come out of
  610. # chained commands which all returned `None` -- so it's not
  611. # even always obvious that `rv` indicates success/failure
  612. # by its truthiness/falsiness
  613. ctx.exit()
  614. except (EOFError, KeyboardInterrupt):
  615. echo(file=sys.stderr)
  616. raise Abort()
  617. except ClickException as e:
  618. if not standalone_mode:
  619. raise
  620. e.show()
  621. sys.exit(e.exit_code)
  622. except IOError as e:
  623. if e.errno == errno.EPIPE:
  624. sys.stdout = PacifyFlushWrapper(sys.stdout)
  625. sys.stderr = PacifyFlushWrapper(sys.stderr)
  626. sys.exit(1)
  627. else:
  628. raise
  629. except Exit as e:
  630. if standalone_mode:
  631. sys.exit(e.exit_code)
  632. else:
  633. # in non-standalone mode, return the exit code
  634. # note that this is only reached if `self.invoke` above raises
  635. # an Exit explicitly -- thus bypassing the check there which
  636. # would return its result
  637. # the results of non-standalone execution may therefore be
  638. # somewhat ambiguous: if there are codepaths which lead to
  639. # `ctx.exit(1)` and to `return 1`, the caller won't be able to
  640. # tell the difference between the two
  641. return e.exit_code
  642. except Abort:
  643. if not standalone_mode:
  644. raise
  645. echo('Aborted!', file=sys.stderr)
  646. sys.exit(1)
  647. def __call__(self, *args, **kwargs):
  648. """Alias for :meth:`main`."""
  649. return self.main(*args, **kwargs)
  650. class Command(BaseCommand):
  651. """Commands are the basic building block of command line interfaces in
  652. Click. A basic command handles command line parsing and might dispatch
  653. more parsing to commands nested below it.
  654. .. versionchanged:: 2.0
  655. Added the `context_settings` parameter.
  656. :param name: the name of the command to use unless a group overrides it.
  657. :param context_settings: an optional dictionary with defaults that are
  658. passed to the context object.
  659. :param callback: the callback to invoke. This is optional.
  660. :param params: the parameters to register with this command. This can
  661. be either :class:`Option` or :class:`Argument` objects.
  662. :param help: the help string to use for this command.
  663. :param epilog: like the help string but it's printed at the end of the
  664. help page after everything else.
  665. :param short_help: the short help to use for this command. This is
  666. shown on the command listing of the parent command.
  667. :param add_help_option: by default each command registers a ``--help``
  668. option. This can be disabled by this parameter.
  669. :param hidden: hide this command from help outputs.
  670. :param deprecated: issues a message indicating that
  671. the command is deprecated.
  672. """
  673. def __init__(self, name, context_settings=None, callback=None,
  674. params=None, help=None, epilog=None, short_help=None,
  675. options_metavar='[OPTIONS]', add_help_option=True,
  676. hidden=False, deprecated=False):
  677. BaseCommand.__init__(self, name, context_settings)
  678. #: the callback to execute when the command fires. This might be
  679. #: `None` in which case nothing happens.
  680. self.callback = callback
  681. #: the list of parameters for this command in the order they
  682. #: should show up in the help page and execute. Eager parameters
  683. #: will automatically be handled before non eager ones.
  684. self.params = params or []
  685. # if a form feed (page break) is found in the help text, truncate help
  686. # text to the content preceding the first form feed
  687. if help and '\f' in help:
  688. help = help.split('\f', 1)[0]
  689. self.help = help
  690. self.epilog = epilog
  691. self.options_metavar = options_metavar
  692. self.short_help = short_help
  693. self.add_help_option = add_help_option
  694. self.hidden = hidden
  695. self.deprecated = deprecated
  696. def get_usage(self, ctx):
  697. formatter = ctx.make_formatter()
  698. self.format_usage(ctx, formatter)
  699. return formatter.getvalue().rstrip('\n')
  700. def get_params(self, ctx):
  701. rv = self.params
  702. help_option = self.get_help_option(ctx)
  703. if help_option is not None:
  704. rv = rv + [help_option]
  705. return rv
  706. def format_usage(self, ctx, formatter):
  707. """Writes the usage line into the formatter."""
  708. pieces = self.collect_usage_pieces(ctx)
  709. formatter.write_usage(ctx.command_path, ' '.join(pieces))
  710. def collect_usage_pieces(self, ctx):
  711. """Returns all the pieces that go into the usage line and returns
  712. it as a list of strings.
  713. """
  714. rv = [self.options_metavar]
  715. for param in self.get_params(ctx):
  716. rv.extend(param.get_usage_pieces(ctx))
  717. return rv
  718. def get_help_option_names(self, ctx):
  719. """Returns the names for the help option."""
  720. all_names = set(ctx.help_option_names)
  721. for param in self.params:
  722. all_names.difference_update(param.opts)
  723. all_names.difference_update(param.secondary_opts)
  724. return all_names
  725. def get_help_option(self, ctx):
  726. """Returns the help option object."""
  727. help_options = self.get_help_option_names(ctx)
  728. if not help_options or not self.add_help_option:
  729. return
  730. def show_help(ctx, param, value):
  731. if value and not ctx.resilient_parsing:
  732. echo(ctx.get_help(), color=ctx.color)
  733. ctx.exit()
  734. return Option(help_options, is_flag=True,
  735. is_eager=True, expose_value=False,
  736. callback=show_help,
  737. help='Show this message and exit.')
  738. def make_parser(self, ctx):
  739. """Creates the underlying option parser for this command."""
  740. parser = OptionParser(ctx)
  741. for param in self.get_params(ctx):
  742. param.add_to_parser(parser, ctx)
  743. return parser
  744. def get_help(self, ctx):
  745. """Formats the help into a string and returns it. This creates a
  746. formatter and will call into the following formatting methods:
  747. """
  748. formatter = ctx.make_formatter()
  749. self.format_help(ctx, formatter)
  750. return formatter.getvalue().rstrip('\n')
  751. def get_short_help_str(self, limit=45):
  752. """Gets short help for the command or makes it by shortening the long help string."""
  753. return self.short_help or self.help and make_default_short_help(self.help, limit) or ''
  754. def format_help(self, ctx, formatter):
  755. """Writes the help into the formatter if it exists.
  756. This calls into the following methods:
  757. - :meth:`format_usage`
  758. - :meth:`format_help_text`
  759. - :meth:`format_options`
  760. - :meth:`format_epilog`
  761. """
  762. self.format_usage(ctx, formatter)
  763. self.format_help_text(ctx, formatter)
  764. self.format_options(ctx, formatter)
  765. self.format_epilog(ctx, formatter)
  766. def format_help_text(self, ctx, formatter):
  767. """Writes the help text to the formatter if it exists."""
  768. if self.help:
  769. formatter.write_paragraph()
  770. with formatter.indentation():
  771. help_text = self.help
  772. if self.deprecated:
  773. help_text += DEPRECATED_HELP_NOTICE
  774. formatter.write_text(help_text)
  775. elif self.deprecated:
  776. formatter.write_paragraph()
  777. with formatter.indentation():
  778. formatter.write_text(DEPRECATED_HELP_NOTICE)
  779. def format_options(self, ctx, formatter):
  780. """Writes all the options into the formatter if they exist."""
  781. opts = []
  782. for param in self.get_params(ctx):
  783. rv = param.get_help_record(ctx)
  784. if rv is not None:
  785. opts.append(rv)
  786. if opts:
  787. with formatter.section('Options'):
  788. formatter.write_dl(opts)
  789. def format_epilog(self, ctx, formatter):
  790. """Writes the epilog into the formatter if it exists."""
  791. if self.epilog:
  792. formatter.write_paragraph()
  793. with formatter.indentation():
  794. formatter.write_text(self.epilog)
  795. def parse_args(self, ctx, args):
  796. parser = self.make_parser(ctx)
  797. opts, args, param_order = parser.parse_args(args=args)
  798. for param in iter_params_for_processing(
  799. param_order, self.get_params(ctx)):
  800. value, args = param.handle_parse_result(ctx, opts, args)
  801. if args and not ctx.allow_extra_args and not ctx.resilient_parsing:
  802. ctx.fail('Got unexpected extra argument%s (%s)'
  803. % (len(args) != 1 and 's' or '',
  804. ' '.join(map(make_str, args))))
  805. ctx.args = args
  806. return args
  807. def invoke(self, ctx):
  808. """Given a context, this invokes the attached callback (if it exists)
  809. in the right way.
  810. """
  811. _maybe_show_deprecated_notice(self)
  812. if self.callback is not None:
  813. return ctx.invoke(self.callback, **ctx.params)
  814. class MultiCommand(Command):
  815. """A multi command is the basic implementation of a command that
  816. dispatches to subcommands. The most common version is the
  817. :class:`Group`.
  818. :param invoke_without_command: this controls how the multi command itself
  819. is invoked. By default it's only invoked
  820. if a subcommand is provided.
  821. :param no_args_is_help: this controls what happens if no arguments are
  822. provided. This option is enabled by default if
  823. `invoke_without_command` is disabled or disabled
  824. if it's enabled. If enabled this will add
  825. ``--help`` as argument if no arguments are
  826. passed.
  827. :param subcommand_metavar: the string that is used in the documentation
  828. to indicate the subcommand place.
  829. :param chain: if this is set to `True` chaining of multiple subcommands
  830. is enabled. This restricts the form of commands in that
  831. they cannot have optional arguments but it allows
  832. multiple commands to be chained together.
  833. :param result_callback: the result callback to attach to this multi
  834. command.
  835. """
  836. allow_extra_args = True
  837. allow_interspersed_args = False
  838. def __init__(self, name=None, invoke_without_command=False,
  839. no_args_is_help=None, subcommand_metavar=None,
  840. chain=False, result_callback=None, **attrs):
  841. Command.__init__(self, name, **attrs)
  842. if no_args_is_help is None:
  843. no_args_is_help = not invoke_without_command
  844. self.no_args_is_help = no_args_is_help
  845. self.invoke_without_command = invoke_without_command
  846. if subcommand_metavar is None:
  847. if chain:
  848. subcommand_metavar = SUBCOMMANDS_METAVAR
  849. else:
  850. subcommand_metavar = SUBCOMMAND_METAVAR
  851. self.subcommand_metavar = subcommand_metavar
  852. self.chain = chain
  853. #: The result callback that is stored. This can be set or
  854. #: overridden with the :func:`resultcallback` decorator.
  855. self.result_callback = result_callback
  856. if self.chain:
  857. for param in self.params:
  858. if isinstance(param, Argument) and not param.required:
  859. raise RuntimeError('Multi commands in chain mode cannot '
  860. 'have optional arguments.')
  861. def collect_usage_pieces(self, ctx):
  862. rv = Command.collect_usage_pieces(self, ctx)
  863. rv.append(self.subcommand_metavar)
  864. return rv
  865. def format_options(self, ctx, formatter):
  866. Command.format_options(self, ctx, formatter)
  867. self.format_commands(ctx, formatter)
  868. def resultcallback(self, replace=False):
  869. """Adds a result callback to the chain command. By default if a
  870. result callback is already registered this will chain them but
  871. this can be disabled with the `replace` parameter. The result
  872. callback is invoked with the return value of the subcommand
  873. (or the list of return values from all subcommands if chaining
  874. is enabled) as well as the parameters as they would be passed
  875. to the main callback.
  876. Example::
  877. @click.group()
  878. @click.option('-i', '--input', default=23)
  879. def cli(input):
  880. return 42
  881. @cli.resultcallback()
  882. def process_result(result, input):
  883. return result + input
  884. .. versionadded:: 3.0
  885. :param replace: if set to `True` an already existing result
  886. callback will be removed.
  887. """
  888. def decorator(f):
  889. old_callback = self.result_callback
  890. if old_callback is None or replace:
  891. self.result_callback = f
  892. return f
  893. def function(__value, *args, **kwargs):
  894. return f(old_callback(__value, *args, **kwargs),
  895. *args, **kwargs)
  896. self.result_callback = rv = update_wrapper(function, f)
  897. return rv
  898. return decorator
  899. def format_commands(self, ctx, formatter):
  900. """Extra format methods for multi methods that adds all the commands
  901. after the options.
  902. """
  903. commands = []
  904. for subcommand in self.list_commands(ctx):
  905. cmd = self.get_command(ctx, subcommand)
  906. # What is this, the tool lied about a command. Ignore it
  907. if cmd is None:
  908. continue
  909. if cmd.hidden:
  910. continue
  911. commands.append((subcommand, cmd))
  912. # allow for 3 times the default spacing
  913. if len(commands):
  914. limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)
  915. rows = []
  916. for subcommand, cmd in commands:
  917. help = cmd.get_short_help_str(limit)
  918. rows.append((subcommand, help))
  919. if rows:
  920. with formatter.section('Commands'):
  921. formatter.write_dl(rows)
  922. def parse_args(self, ctx, args):
  923. if not args and self.no_args_is_help and not ctx.resilient_parsing:
  924. echo(ctx.get_help(), color=ctx.color)
  925. ctx.exit()
  926. rest = Command.parse_args(self, ctx, args)
  927. if self.chain:
  928. ctx.protected_args = rest
  929. ctx.args = []
  930. elif rest:
  931. ctx.protected_args, ctx.args = rest[:1], rest[1:]
  932. return ctx.args
  933. def invoke(self, ctx):
  934. def _process_result(value):
  935. if self.result_callback is not None:
  936. value = ctx.invoke(self.result_callback, value,
  937. **ctx.params)
  938. return value
  939. if not ctx.protected_args:
  940. # If we are invoked without command the chain flag controls
  941. # how this happens. If we are not in chain mode, the return
  942. # value here is the return value of the command.
  943. # If however we are in chain mode, the return value is the
  944. # return value of the result processor invoked with an empty
  945. # list (which means that no subcommand actually was executed).
  946. if self.invoke_without_command:
  947. if not self.chain:
  948. return Command.invoke(self, ctx)
  949. with ctx:
  950. Command.invoke(self, ctx)
  951. return _process_result([])
  952. ctx.fail('Missing command.')
  953. # Fetch args back out
  954. args = ctx.protected_args + ctx.args
  955. ctx.args = []
  956. ctx.protected_args = []
  957. # If we're not in chain mode, we only allow the invocation of a
  958. # single command but we also inform the current context about the
  959. # name of the command to invoke.
  960. if not self.chain:
  961. # Make sure the context is entered so we do not clean up
  962. # resources until the result processor has worked.
  963. with ctx:
  964. cmd_name, cmd, args = self.resolve_command(ctx, args)
  965. ctx.invoked_subcommand = cmd_name
  966. Command.invoke(self, ctx)
  967. sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  968. with sub_ctx:
  969. return _process_result(sub_ctx.command.invoke(sub_ctx))
  970. # In chain mode we create the contexts step by step, but after the
  971. # base command has been invoked. Because at that point we do not
  972. # know the subcommands yet, the invoked subcommand attribute is
  973. # set to ``*`` to inform the command that subcommands are executed
  974. # but nothing else.
  975. with ctx:
  976. ctx.invoked_subcommand = args and '*' or None
  977. Command.invoke(self, ctx)
  978. # Otherwise we make every single context and invoke them in a
  979. # chain. In that case the return value to the result processor
  980. # is the list of all invoked subcommand's results.
  981. contexts = []
  982. while args:
  983. cmd_name, cmd, args = self.resolve_command(ctx, args)
  984. sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,
  985. allow_extra_args=True,
  986. allow_interspersed_args=False)
  987. contexts.append(sub_ctx)
  988. args, sub_ctx.args = sub_ctx.args, []
  989. rv = []
  990. for sub_ctx in contexts:
  991. with sub_ctx:
  992. rv.append(sub_ctx.command.invoke(sub_ctx))
  993. return _process_result(rv)
  994. def resolve_command(self, ctx, args):
  995. cmd_name = make_str(args[0])
  996. original_cmd_name = cmd_name
  997. # Get the command
  998. cmd = self.get_command(ctx, cmd_name)
  999. # If we can't find the command but there is a normalization
  1000. # function available, we try with that one.
  1001. if cmd is None and ctx.token_normalize_func is not None:
  1002. cmd_name = ctx.token_normalize_func(cmd_name)
  1003. cmd = self.get_command(ctx, cmd_name)
  1004. # If we don't find the command we want to show an error message
  1005. # to the user that it was not provided. However, there is
  1006. # something else we should do: if the first argument looks like
  1007. # an option we want to kick off parsing again for arguments to
  1008. # resolve things like --help which now should go to the main
  1009. # place.
  1010. if cmd is None and not ctx.resilient_parsing:
  1011. if split_opt(cmd_name)[0]:
  1012. self.parse_args(ctx, ctx.args)
  1013. ctx.fail('No such command "%s".' % original_cmd_name)
  1014. return cmd_name, cmd, args[1:]
  1015. def get_command(self, ctx, cmd_name):
  1016. """Given a context and a command name, this returns a
  1017. :class:`Command` object if it exists or returns `None`.
  1018. """
  1019. raise NotImplementedError()
  1020. def list_commands(self, ctx):
  1021. """Returns a list of subcommand names in the order they should
  1022. appear.
  1023. """
  1024. return []
  1025. class Group(MultiCommand):
  1026. """A group allows a command to have subcommands attached. This is the
  1027. most common way to implement nesting in Click.
  1028. :param commands: a dictionary of commands.
  1029. """
  1030. def __init__(self, name=None, commands=None, **attrs):
  1031. MultiCommand.__init__(self, name, **attrs)
  1032. #: the registered subcommands by their exported names.
  1033. self.commands = commands or {}
  1034. def add_command(self, cmd, name=None):
  1035. """Registers another :class:`Command` with this group. If the name
  1036. is not provided, the name of the command is used.
  1037. """
  1038. name = name or cmd.name
  1039. if name is None:
  1040. raise TypeError('Command has no name.')
  1041. _check_multicommand(self, name, cmd, register=True)
  1042. self.commands[name] = cmd
  1043. def command(self, *args, **kwargs):
  1044. """A shortcut decorator for declaring and attaching a command to
  1045. the group. This takes the same arguments as :func:`command` but
  1046. immediately registers the created command with this instance by
  1047. calling into :meth:`add_command`.
  1048. """
  1049. def decorator(f):
  1050. cmd = command(*args, **kwargs)(f)
  1051. self.add_command(cmd)
  1052. return cmd
  1053. return decorator
  1054. def group(self, *args, **kwargs):
  1055. """A shortcut decorator for declaring and attaching a group to
  1056. the group. This takes the same arguments as :func:`group` but
  1057. immediately registers the created command with this instance by
  1058. calling into :meth:`add_command`.
  1059. """
  1060. def decorator(f):
  1061. cmd = group(*args, **kwargs)(f)
  1062. self.add_command(cmd)
  1063. return cmd
  1064. return decorator
  1065. def get_command(self, ctx, cmd_name):
  1066. return self.commands.get(cmd_name)
  1067. def list_commands(self, ctx):
  1068. return sorted(self.commands)
  1069. class CommandCollection(MultiCommand):
  1070. """A command collection is a multi command that merges multiple multi
  1071. commands together into one. This is a straightforward implementation
  1072. that accepts a list of different multi commands as sources and
  1073. provides all the commands for each of them.
  1074. """
  1075. def __init__(self, name=None, sources=None, **attrs):
  1076. MultiCommand.__init__(self, name, **attrs)
  1077. #: The list of registered multi commands.
  1078. self.sources = sources or []
  1079. def add_source(self, multi_cmd):
  1080. """Adds a new multi command to the chain dispatcher."""
  1081. self.sources.append(multi_cmd)
  1082. def get_command(self, ctx, cmd_name):
  1083. for source in self.sources:
  1084. rv = source.get_command(ctx, cmd_name)
  1085. if rv is not None:
  1086. if self.chain:
  1087. _check_multicommand(self, cmd_name, rv)
  1088. return rv
  1089. def list_commands(self, ctx):
  1090. rv = set()
  1091. for source in self.sources:
  1092. rv.update(source.list_commands(ctx))
  1093. return sorted(rv)
  1094. class Parameter(object):
  1095. r"""A parameter to a command comes in two versions: they are either
  1096. :class:`Option`\s or :class:`Argument`\s. Other subclasses are currently
  1097. not supported by design as some of the internals for parsing are
  1098. intentionally not finalized.
  1099. Some settings are supported by both options and arguments.
  1100. .. versionchanged:: 2.0
  1101. Changed signature for parameter callback to also be passed the
  1102. parameter. In Click 2.0, the old callback format will still work,
  1103. but it will raise a warning to give you change to migrate the
  1104. code easier.
  1105. :param param_decls: the parameter declarations for this option or
  1106. argument. This is a list of flags or argument
  1107. names.
  1108. :param type: the type that should be used. Either a :class:`ParamType`
  1109. or a Python type. The later is converted into the former
  1110. automatically if supported.
  1111. :param required: controls if this is optional or not.
  1112. :param default: the default value if omitted. This can also be a callable,
  1113. in which case it's invoked when the default is needed
  1114. without any arguments.
  1115. :param callback: a callback that should be executed after the parameter
  1116. was matched. This is called as ``fn(ctx, param,
  1117. value)`` and needs to return the value. Before Click
  1118. 2.0, the signature was ``(ctx, value)``.
  1119. :param nargs: the number of arguments to match. If not ``1`` the return
  1120. value is a tuple instead of single value. The default for
  1121. nargs is ``1`` (except if the type is a tuple, then it's
  1122. the arity of the tuple).
  1123. :param metavar: how the value is represented in the help page.
  1124. :param expose_value: if this is `True` then the value is passed onwards
  1125. to the command callback and stored on the context,
  1126. otherwise it's skipped.
  1127. :param is_eager: eager values are processed before non eager ones. This
  1128. should not be set for arguments or it will inverse the
  1129. order of processing.
  1130. :param envvar: a string or list of strings that are environment variables
  1131. that should be checked.
  1132. """
  1133. param_type_name = 'parameter'
  1134. def __init__(self, param_decls=None, type=None, required=False,
  1135. default=None, callback=None, nargs=None, metavar=None,
  1136. expose_value=True, is_eager=False, envvar=None,
  1137. autocompletion=None):
  1138. self.name, self.opts, self.secondary_opts = \
  1139. self._parse_decls(param_decls or (), expose_value)
  1140. self.type = convert_type(type, default)
  1141. # Default nargs to what the type tells us if we have that
  1142. # information available.
  1143. if nargs is None:
  1144. if self.type.is_composite:
  1145. nargs = self.type.arity
  1146. else:
  1147. nargs = 1
  1148. self.required = required
  1149. self.callback = callback
  1150. self.nargs = nargs
  1151. self.multiple = False
  1152. self.expose_value = expose_value
  1153. self.default = default
  1154. self.is_eager = is_eager
  1155. self.metavar = metavar
  1156. self.envvar = envvar
  1157. self.autocompletion = autocompletion
  1158. @property
  1159. def human_readable_name(self):
  1160. """Returns the human readable name of this parameter. This is the
  1161. same as the name for options, but the metavar for arguments.
  1162. """
  1163. return self.name
  1164. def make_metavar(self):
  1165. if self.metavar is not None:
  1166. return self.metavar
  1167. metavar = self.type.get_metavar(self)
  1168. if metavar is None:
  1169. metavar = self.type.name.upper()
  1170. if self.nargs != 1:
  1171. metavar += '...'
  1172. return metavar
  1173. def get_default(self, ctx):
  1174. """Given a context variable this calculates the default value."""
  1175. # Otherwise go with the regular default.
  1176. if callable(self.default):
  1177. rv = self.default()
  1178. else:
  1179. rv = self.default
  1180. return self.type_cast_value(ctx, rv)
  1181. def add_to_parser(self, parser, ctx):
  1182. pass
  1183. def consume_value(self, ctx, opts):
  1184. value = opts.get(self.name)
  1185. if value is None:
  1186. value = self.value_from_envvar(ctx)
  1187. if value is None:
  1188. value = ctx.lookup_default(self.name)
  1189. return value
  1190. def type_cast_value(self, ctx, value):
  1191. """Given a value this runs it properly through the type system.
  1192. This automatically handles things like `nargs` and `multiple` as
  1193. well as composite types.
  1194. """
  1195. if self.type.is_composite:
  1196. if self.nargs <= 1:
  1197. raise TypeError('Attempted to invoke composite type '
  1198. 'but nargs has been set to %s. This is '
  1199. 'not supported; nargs needs to be set to '
  1200. 'a fixed value > 1.' % self.nargs)
  1201. if self.multiple:
  1202. return tuple(self.type(x or (), self, ctx) for x in value or ())
  1203. return self.type(value or (), self, ctx)
  1204. def _convert(value, level):
  1205. if level == 0:
  1206. return self.type(value, self, ctx)
  1207. return tuple(_convert(x, level - 1) for x in value or ())
  1208. return _convert(value, (self.nargs != 1) + bool(self.multiple))
  1209. def process_value(self, ctx, value):
  1210. """Given a value and context this runs the logic to convert the
  1211. value as necessary.
  1212. """
  1213. # If the value we were given is None we do nothing. This way
  1214. # code that calls this can easily figure out if something was
  1215. # not provided. Otherwise it would be converted into an empty
  1216. # tuple for multiple invocations which is inconvenient.
  1217. if value is not None:
  1218. return self.type_cast_value(ctx, value)
  1219. def value_is_missing(self, value):
  1220. if value is None:
  1221. return True
  1222. if (self.nargs != 1 or self.multiple) and value == ():
  1223. return True
  1224. return False
  1225. def full_process_value(self, ctx, value):
  1226. value = self.process_value(ctx, value)
  1227. if value is None and not ctx.resilient_parsing:
  1228. value = self.get_default(ctx)
  1229. if self.required and self.value_is_missing(value):
  1230. raise MissingParameter(ctx=ctx, param=self)
  1231. return value
  1232. def resolve_envvar_value(self, ctx):
  1233. if self.envvar is None:
  1234. return
  1235. if isinstance(self.envvar, (tuple, list)):
  1236. for envvar in self.envvar:
  1237. rv = os.environ.get(envvar)
  1238. if rv is not None:
  1239. return rv
  1240. else:
  1241. return os.environ.get(self.envvar)
  1242. def value_from_envvar(self, ctx):
  1243. rv = self.resolve_envvar_value(ctx)
  1244. if rv is not None and self.nargs != 1:
  1245. rv = self.type.split_envvar_value(rv)
  1246. return rv
  1247. def handle_parse_result(self, ctx, opts, args):
  1248. with augment_usage_errors(ctx, param=self):
  1249. value = self.consume_value(ctx, opts)
  1250. try:
  1251. value = self.full_process_value(ctx, value)
  1252. except Exception:
  1253. if not ctx.resilient_parsing:
  1254. raise
  1255. value = None
  1256. if self.callback is not None:
  1257. try:
  1258. value = invoke_param_callback(
  1259. self.callback, ctx, self, value)
  1260. except Exception:
  1261. if not ctx.resilient_parsing:
  1262. raise
  1263. if self.expose_value:
  1264. ctx.params[self.name] = value
  1265. return value, args
  1266. def get_help_record(self, ctx):
  1267. pass
  1268. def get_usage_pieces(self, ctx):
  1269. return []
  1270. def get_error_hint(self, ctx):
  1271. """Get a stringified version of the param for use in error messages to
  1272. indicate which param caused the error.
  1273. """
  1274. hint_list = self.opts or [self.human_readable_name]
  1275. return ' / '.join('"%s"' % x for x in hint_list)
  1276. class Option(Parameter):
  1277. """Options are usually optional values on the command line and
  1278. have some extra features that arguments don't have.
  1279. All other parameters are passed onwards to the parameter constructor.
  1280. :param show_default: controls if the default value should be shown on the
  1281. help page. Normally, defaults are not shown. If this
  1282. value is a string, it shows the string instead of the
  1283. value. This is particularly useful for dynamic options.
  1284. :param show_envvar: controls if an environment variable should be shown on
  1285. the help page. Normally, environment variables
  1286. are not shown.
  1287. :param prompt: if set to `True` or a non empty string then the user will be
  1288. prompted for input. If set to `True` the prompt will be the
  1289. option name capitalized.
  1290. :param confirmation_prompt: if set then the value will need to be confirmed
  1291. if it was prompted for.
  1292. :param hide_input: if this is `True` then the input on the prompt will be
  1293. hidden from the user. This is useful for password
  1294. input.
  1295. :param is_flag: forces this option to act as a flag. The default is
  1296. auto detection.
  1297. :param flag_value: which value should be used for this flag if it's
  1298. enabled. This is set to a boolean automatically if
  1299. the option string contains a slash to mark two options.
  1300. :param multiple: if this is set to `True` then the argument is accepted
  1301. multiple times and recorded. This is similar to ``nargs``
  1302. in how it works but supports arbitrary number of
  1303. arguments.
  1304. :param count: this flag makes an option increment an integer.
  1305. :param allow_from_autoenv: if this is enabled then the value of this
  1306. parameter will be pulled from an environment
  1307. variable in case a prefix is defined on the
  1308. context.
  1309. :param help: the help string.
  1310. :param hidden: hide this option from help outputs.
  1311. """
  1312. param_type_name = 'option'
  1313. def __init__(self, param_decls=None, show_default=False,
  1314. prompt=False, confirmation_prompt=False,
  1315. hide_input=False, is_flag=None, flag_value=None,
  1316. multiple=False, count=False, allow_from_autoenv=True,
  1317. type=None, help=None, hidden=False, show_choices=True,
  1318. show_envvar=False, **attrs):
  1319. default_is_missing = attrs.get('default', _missing) is _missing
  1320. Parameter.__init__(self, param_decls, type=type, **attrs)
  1321. if prompt is True:
  1322. prompt_text = self.name.replace('_', ' ').capitalize()
  1323. elif prompt is False:
  1324. prompt_text = None
  1325. else:
  1326. prompt_text = prompt
  1327. self.prompt = prompt_text
  1328. self.confirmation_prompt = confirmation_prompt
  1329. self.hide_input = hide_input
  1330. self.hidden = hidden
  1331. # Flags
  1332. if is_flag is None:
  1333. if flag_value is not None:
  1334. is_flag = True
  1335. else:
  1336. is_flag = bool(self.secondary_opts)
  1337. if is_flag and default_is_missing:
  1338. self.default = False
  1339. if flag_value is None:
  1340. flag_value = not self.default
  1341. self.is_flag = is_flag
  1342. self.flag_value = flag_value
  1343. if self.is_flag and isinstance(self.flag_value, bool) \
  1344. and type is None:
  1345. self.type = BOOL
  1346. self.is_bool_flag = True
  1347. else:
  1348. self.is_bool_flag = False
  1349. # Counting
  1350. self.count = count
  1351. if count:
  1352. if type is None:
  1353. self.type = IntRange(min=0)
  1354. if default_is_missing:
  1355. self.default = 0
  1356. self.multiple = multiple
  1357. self.allow_from_autoenv = allow_from_autoenv
  1358. self.help = help
  1359. self.show_default = show_default
  1360. self.show_choices = show_choices
  1361. self.show_envvar = show_envvar
  1362. # Sanity check for stuff we don't support
  1363. if __debug__:
  1364. if self.nargs < 0:
  1365. raise TypeError('Options cannot have nargs < 0')
  1366. if self.prompt and self.is_flag and not self.is_bool_flag:
  1367. raise TypeError('Cannot prompt for flags that are not bools.')
  1368. if not self.is_bool_flag and self.secondary_opts:
  1369. raise TypeError('Got secondary option for non boolean flag.')
  1370. if self.is_bool_flag and self.hide_input \
  1371. and self.prompt is not None:
  1372. raise TypeError('Hidden input does not work with boolean '
  1373. 'flag prompts.')
  1374. if self.count:
  1375. if self.multiple:
  1376. raise TypeError('Options cannot be multiple and count '
  1377. 'at the same time.')
  1378. elif self.is_flag:
  1379. raise TypeError('Options cannot be count and flags at '
  1380. 'the same time.')
  1381. def _parse_decls(self, decls, expose_value):
  1382. opts = []
  1383. secondary_opts = []
  1384. name = None
  1385. possible_names = []
  1386. for decl in decls:
  1387. if isidentifier(decl):
  1388. if name is not None:
  1389. raise TypeError('Name defined twice')
  1390. name = decl
  1391. else:
  1392. split_char = decl[:1] == '/' and ';' or '/'
  1393. if split_char in decl:
  1394. first, second = decl.split(split_char, 1)
  1395. first = first.rstrip()
  1396. if first:
  1397. possible_names.append(split_opt(first))
  1398. opts.append(first)
  1399. second = second.lstrip()
  1400. if second:
  1401. secondary_opts.append(second.lstrip())
  1402. else:
  1403. possible_names.append(split_opt(decl))
  1404. opts.append(decl)
  1405. if name is None and possible_names:
  1406. possible_names.sort(key=lambda x: -len(x[0])) # group long options first
  1407. name = possible_names[0][1].replace('-', '_').lower()
  1408. if not isidentifier(name):
  1409. name = None
  1410. if name is None:
  1411. if not expose_value:
  1412. return None, opts, secondary_opts
  1413. raise TypeError('Could not determine name for option')
  1414. if not opts and not secondary_opts:
  1415. raise TypeError('No options defined but a name was passed (%s). '
  1416. 'Did you mean to declare an argument instead '
  1417. 'of an option?' % name)
  1418. return name, opts, secondary_opts
  1419. def add_to_parser(self, parser, ctx):
  1420. kwargs = {
  1421. 'dest': self.name,
  1422. 'nargs': self.nargs,
  1423. 'obj': self,
  1424. }
  1425. if self.multiple:
  1426. action = 'append'
  1427. elif self.count:
  1428. action = 'count'
  1429. else:
  1430. action = 'store'
  1431. if self.is_flag:
  1432. kwargs.pop('nargs', None)
  1433. if self.is_bool_flag and self.secondary_opts:
  1434. parser.add_option(self.opts, action=action + '_const',
  1435. const=True, **kwargs)
  1436. parser.add_option(self.secondary_opts, action=action +
  1437. '_const', const=False, **kwargs)
  1438. else:
  1439. parser.add_option(self.opts, action=action + '_const',
  1440. const=self.flag_value,
  1441. **kwargs)
  1442. else:
  1443. kwargs['action'] = action
  1444. parser.add_option(self.opts, **kwargs)
  1445. def get_help_record(self, ctx):
  1446. if self.hidden:
  1447. return
  1448. any_prefix_is_slash = []
  1449. def _write_opts(opts):
  1450. rv, any_slashes = join_options(opts)
  1451. if any_slashes:
  1452. any_prefix_is_slash[:] = [True]
  1453. if not self.is_flag and not self.count:
  1454. rv += ' ' + self.make_metavar()
  1455. return rv
  1456. rv = [_write_opts(self.opts)]
  1457. if self.secondary_opts:
  1458. rv.append(_write_opts(self.secondary_opts))
  1459. help = self.help or ''
  1460. extra = []
  1461. if self.show_envvar:
  1462. envvar = self.envvar
  1463. if envvar is None:
  1464. if self.allow_from_autoenv and \
  1465. ctx.auto_envvar_prefix is not None:
  1466. envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())
  1467. if envvar is not None:
  1468. extra.append('env var: %s' % (
  1469. ', '.join('%s' % d for d in envvar)
  1470. if isinstance(envvar, (list, tuple))
  1471. else envvar, ))
  1472. if self.default is not None and self.show_default:
  1473. if isinstance(self.show_default, string_types):
  1474. default_string = '({})'.format(self.show_default)
  1475. elif isinstance(self.default, (list, tuple)):
  1476. default_string = ', '.join('%s' % d for d in self.default)
  1477. elif inspect.isfunction(self.default):
  1478. default_string = "(dynamic)"
  1479. else:
  1480. default_string = self.default
  1481. extra.append('default: {}'.format(default_string))
  1482. if self.required:
  1483. extra.append('required')
  1484. if extra:
  1485. help = '%s[%s]' % (help and help + ' ' or '', '; '.join(extra))
  1486. return ((any_prefix_is_slash and '; ' or ' / ').join(rv), help)
  1487. def get_default(self, ctx):
  1488. # If we're a non boolean flag out default is more complex because
  1489. # we need to look at all flags in the same group to figure out
  1490. # if we're the the default one in which case we return the flag
  1491. # value as default.
  1492. if self.is_flag and not self.is_bool_flag:
  1493. for param in ctx.command.params:
  1494. if param.name == self.name and param.default:
  1495. return param.flag_value
  1496. return None
  1497. return Parameter.get_default(self, ctx)
  1498. def prompt_for_value(self, ctx):
  1499. """This is an alternative flow that can be activated in the full
  1500. value processing if a value does not exist. It will prompt the
  1501. user until a valid value exists and then returns the processed
  1502. value as result.
  1503. """
  1504. # Calculate the default before prompting anything to be stable.
  1505. default = self.get_default(ctx)
  1506. # If this is a prompt for a flag we need to handle this
  1507. # differently.
  1508. if self.is_bool_flag:
  1509. return confirm(self.prompt, default)
  1510. return prompt(self.prompt, default=default, type=self.type,
  1511. hide_input=self.hide_input, show_choices=self.show_choices,
  1512. confirmation_prompt=self.confirmation_prompt,
  1513. value_proc=lambda x: self.process_value(ctx, x))
  1514. def resolve_envvar_value(self, ctx):
  1515. rv = Parameter.resolve_envvar_value(self, ctx)
  1516. if rv is not None:
  1517. return rv
  1518. if self.allow_from_autoenv and \
  1519. ctx.auto_envvar_prefix is not None:
  1520. envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())
  1521. return os.environ.get(envvar)
  1522. def value_from_envvar(self, ctx):
  1523. rv = self.resolve_envvar_value(ctx)
  1524. if rv is None:
  1525. return None
  1526. value_depth = (self.nargs != 1) + bool(self.multiple)
  1527. if value_depth > 0 and rv is not None:
  1528. rv = self.type.split_envvar_value(rv)
  1529. if self.multiple and self.nargs != 1:
  1530. rv = batch(rv, self.nargs)
  1531. return rv
  1532. def full_process_value(self, ctx, value):
  1533. if value is None and self.prompt is not None \
  1534. and not ctx.resilient_parsing:
  1535. return self.prompt_for_value(ctx)
  1536. return Parameter.full_process_value(self, ctx, value)
  1537. class Argument(Parameter):
  1538. """Arguments are positional parameters to a command. They generally
  1539. provide fewer features than options but can have infinite ``nargs``
  1540. and are required by default.
  1541. All parameters are passed onwards to the parameter constructor.
  1542. """
  1543. param_type_name = 'argument'
  1544. def __init__(self, param_decls, required=None, **attrs):
  1545. if required is None:
  1546. if attrs.get('default') is not None:
  1547. required = False
  1548. else:
  1549. required = attrs.get('nargs', 1) > 0
  1550. Parameter.__init__(self, param_decls, required=required, **attrs)
  1551. if self.default is not None and self.nargs < 0:
  1552. raise TypeError('nargs=-1 in combination with a default value '
  1553. 'is not supported.')
  1554. @property
  1555. def human_readable_name(self):
  1556. if self.metavar is not None:
  1557. return self.metavar
  1558. return self.name.upper()
  1559. def make_metavar(self):
  1560. if self.metavar is not None:
  1561. return self.metavar
  1562. var = self.type.get_metavar(self)
  1563. if not var:
  1564. var = self.name.upper()
  1565. if not self.required:
  1566. var = '[%s]' % var
  1567. if self.nargs != 1:
  1568. var += '...'
  1569. return var
  1570. def _parse_decls(self, decls, expose_value):
  1571. if not decls:
  1572. if not expose_value:
  1573. return None, [], []
  1574. raise TypeError('Could not determine name for argument')
  1575. if len(decls) == 1:
  1576. name = arg = decls[0]
  1577. name = name.replace('-', '_').lower()
  1578. else:
  1579. raise TypeError('Arguments take exactly one '
  1580. 'parameter declaration, got %d' % len(decls))
  1581. return name, [arg], []
  1582. def get_usage_pieces(self, ctx):
  1583. return [self.make_metavar()]
  1584. def get_error_hint(self, ctx):
  1585. return '"%s"' % self.make_metavar()
  1586. def add_to_parser(self, parser, ctx):
  1587. parser.add_argument(dest=self.name, nargs=self.nargs,
  1588. obj=self)
  1589. # Circular dependency between decorators and core
  1590. from .decorators import command, group