blob: 2c693411c757ced0b1e99e49be520f33b2939e60 [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001:mod:`packaging.fancy_getopt` --- Wrapper around the getopt module
2==================================================================
3
4.. module:: packaging.fancy_getopt
5 :synopsis: Additional getopt functionality.
6
7
8.. warning::
9 This module is deprecated and will be replaced with :mod:`optparse`.
10
11This module provides a wrapper around the standard :mod:`getopt` module that
12provides the following additional features:
13
14* short and long options are tied together
15
16* options have help strings, so :func:`fancy_getopt` could potentially create a
17 complete usage summary
18
19* options set attributes of a passed-in object
20
21* boolean options can have "negative aliases" --- e.g. if :option:`--quiet` is
22 the "negative alias" of :option:`--verbose`, then :option:`--quiet` on the
23 command line sets *verbose* to false.
24
25.. function:: fancy_getopt(options, negative_opt, object, args)
26
27 Wrapper function. *options* is a list of ``(long_option, short_option,
28 help_string)`` 3-tuples as described in the constructor for
29 :class:`FancyGetopt`. *negative_opt* should be a dictionary mapping option names
30 to option names, both the key and value should be in the *options* list.
31 *object* is an object which will be used to store values (see the :meth:`getopt`
32 method of the :class:`FancyGetopt` class). *args* is the argument list. Will use
33 ``sys.argv[1:]`` if you pass ``None`` as *args*.
34
35
36.. class:: FancyGetopt([option_table=None])
37
38 The option_table is a list of 3-tuples: ``(long_option, short_option,
39 help_string)``
40
41 If an option takes an argument, its *long_option* should have ``'='`` appended;
42 *short_option* should just be a single character, no ``':'`` in any case.
43 *short_option* should be ``None`` if a *long_option* doesn't have a
44 corresponding *short_option*. All option tuples must have long options.
45
46The :class:`FancyGetopt` class provides the following methods:
47
48
49.. method:: FancyGetopt.getopt([args=None, object=None])
50
51 Parse command-line options in args. Store as attributes on *object*.
52
53 If *args* is ``None`` or not supplied, uses ``sys.argv[1:]``. If *object* is
54 ``None`` or not supplied, creates a new :class:`OptionDummy` instance, stores
55 option values there, and returns a tuple ``(args, object)``. If *object* is
56 supplied, it is modified in place and :func:`getopt` just returns *args*; in
57 both cases, the returned *args* is a modified copy of the passed-in *args* list,
58 which is left untouched.
59
60 .. TODO and args returned are?
61
62
63.. method:: FancyGetopt.get_option_order()
64
65 Returns the list of ``(option, value)`` tuples processed by the previous run of
66 :meth:`getopt` Raises :exc:`RuntimeError` if :meth:`getopt` hasn't been called
67 yet.
68
69
70.. method:: FancyGetopt.generate_help([header=None])
71
72 Generate help text (a list of strings, one per suggested line of output) from
73 the option table for this :class:`FancyGetopt` object.
74
75 If supplied, prints the supplied *header* at the top of the help.