@@ -83,6 +83,22 @@ First run ``brew install bash-completion``, then add the following to
8383 Usage
8484-----
8585
86+ There are two ways of using ``shtab ``:
87+
88+ - `CLI Usage `_: ``shtab ``'s own CLI interface for external applications
89+
90+ - may not require any code modifications whatsoever
91+ - end-users execute ``shtab your_cli_app.your_parser_object ``
92+
93+ - `Library Usage `_: as a library integrated into your CLI application
94+
95+ - adds a couple of lines to your application
96+ - argument mode: end-users execute ``your_cli_app --print-completion {bash,zsh} ``
97+ - subparser mode: end-users execute ``your_cli_app completion {bash,zsh} ``
98+
99+ CLI Usage
100+ ---------
101+
86102The only requirement is that external CLI applications provide an importable
87103``argparse.ArgumentParser `` object (or alternatively an importable function
88104which returns a parser object). This may require a trivial code change.
@@ -203,19 +219,24 @@ appropriate (e.g. ``$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh``).
203219By default, ``shtab `` will silently do nothing if it cannot import the requested
204220application. Use ``-u, --error-unimportable `` to noisily complain.
205221
206- Advanced Configuration
207- ----------------------
222+ Library Usage
223+ -------------
208224
209225See the `examples/ <https://github.com/iterative/shtab/tree/master/examples >`_
210226folder for more.
211227
212228Complex projects with subparsers and custom completions for paths matching
213229certain patterns (e.g. ``--file=*.txt ``) are fully supported (see
230+ `examples/customcomplete.py <https://github.com/iterative/shtab/tree/master/examples/customcomplete.py >`_
231+ or even
214232`iterative/dvc:command/completion.py <https://github.com/iterative/dvc/blob/master/dvc/command/completion.py >`_
215233for example).
216234
217235Add direct support to scripts for a little more configurability:
218236
237+ argparse
238+ ~~~~~~~~
239+
219240.. code :: python
220241
221242 # !/usr/bin/env python
@@ -224,12 +245,7 @@ Add direct support to scripts for a little more configurability:
224245
225246 def get_main_parser ():
226247 parser = argparse.ArgumentParser(prog = " pathcomplete" )
227- parser.add_argument(
228- " -s" ,
229- " --print-completion-shell" ,
230- choices = [" bash" , " zsh" ],
231- help = " prints completion script" ,
232- )
248+ shtab.add_argument_to(parser, [" -s" , " --print-completion" ]) # magic!
233249 # file & directory tab complete
234250 parser.add_argument(" file" , nargs = " ?" ).complete = shtab.FILE
235251 parser.add_argument(" --dir" , default = " ." ).complete = shtab.DIRECTORY
@@ -238,13 +254,7 @@ Add direct support to scripts for a little more configurability:
238254 if __name__ == " __main__" :
239255 parser = get_main_parser()
240256 args = parser.parse_args()
241-
242- # completion magic
243- shell = args.print_completion_shell
244- if shell:
245- print (shtab.complete(parser, shell = shell))
246- else :
247- print (" received <file>=%r --dir=%r " % (args.file, args.dir))
257+ print (" received <file>=%r --dir=%r " % (args.file, args.dir))
248258
249259 docopt
250260~~~~~~
@@ -262,8 +272,6 @@ object from `docopt <https://pypi.org/project/docopt>`_ syntax:
262272
263273 Options:
264274 -g, --goodbye : Say "goodbye" (instead of "hello")
265- -b, --print-bash-completion : Output a bash tab-completion script
266- -z, --print-zsh-completion : Output a zsh tab-completion script
267275
268276 Arguments:
269277 <you> : Your name [default: Anon]
@@ -272,15 +280,9 @@ object from `docopt <https://pypi.org/project/docopt>`_ syntax:
272280 import sys, argopt, shtab # NOQA
273281
274282 parser = argopt.argopt(__doc__ )
283+ shtab.add_argument_to(parser, [" -s" , " --print-completion" ]) # magic!
275284 if __name__ == " __main__" :
276285 args = parser.parse_args()
277- if args.print_bash_completion:
278- print (shtab.complete(parser, shell = " bash" ))
279- sys.exit(0 )
280- if args.print_zsh_completion:
281- print (shtab.complete(parser, shell = " zsh" ))
282- sys.exit(0 )
283-
284286 msg = " k thx bai!" if args.goodbye else " hai!"
285287 print (" {} says '{} ' to {} " .format(args.me, msg, args.you))
286288
0 commit comments