Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 121ff82

Browse files
committed
#1665333: add more docs for optparse.OptionGroup.
1 parent 00d43fd commit 121ff82

1 file changed

Lines changed: 99 additions & 25 deletions

File tree

Doc/library/optparse.rst

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ and :mod:`optparse` will print out a brief summary of your script's options:
6161

6262
.. code-block:: text
6363
64-
usage: <yourscript> [options]
64+
Usage: <yourscript> [options]
6565
66-
options:
66+
Options:
6767
-h, --help show this help message and exit
6868
-f FILE, --file=FILE write report to FILE
6969
-q, --quiet don't print status messages to stdout
@@ -492,9 +492,9 @@ following to standard output:
492492

493493
.. code-block:: text
494494
495-
usage: <yourscript> [options] arg1 arg2
495+
Usage: <yourscript> [options] arg1 arg2
496496
497-
options:
497+
Options:
498498
-h, --help show this help message and exit
499499
-v, --verbose make lots of noise [default]
500500
-q, --quiet be vewwy quiet (I'm hunting wabbits)
@@ -518,7 +518,7 @@ help message:
518518
is then printed before the detailed option help.
519519

520520
If you don't supply a usage string, :mod:`optparse` uses a bland but sensible
521-
default: ``"usage: %prog [options]"``, which is fine if your script doesn't
521+
default: ``"Usage: %prog [options]"``, which is fine if your script doesn't
522522
take any positional arguments.
523523

524524
* every option defines a help string, and doesn't worry about line-wrapping---
@@ -550,12 +550,33 @@ help message:
550550
default value. If an option has no default value (or the default value is
551551
``None``), ``%default`` expands to ``none``.
552552

553+
Grouping Options
554+
++++++++++++++++
555+
553556
When dealing with many options, it is convenient to group these options for
554557
better help output. An :class:`OptionParser` can contain several option groups,
555558
each of which can contain several options.
556559

557-
Continuing with the parser defined above, adding an :class:`OptionGroup` to a
558-
parser is easy::
560+
An option group is obtained using the class :class:`OptionGroup`:
561+
562+
.. class:: OptionGroup(parser, title, description=None)
563+
564+
where
565+
566+
* parser is the :class:`OptionParser` instance the group will be insterted in
567+
to
568+
* title is the group title
569+
* description, optional, is a long description of the group
570+
571+
:class:`OptionGroup` inherits from :class:`OptionContainer` (like
572+
:class:`OptionParser`) and so the :meth:`add_option` method can be used to add
573+
an option to the group.
574+
575+
Once all the options are declared, using the :class:`OptionParser` method
576+
:meth:`add_option_group` the group is added to the previously defined parser.
577+
578+
Continuing with the parser defined in the previous section, adding an
579+
:class:`OptionGroup` to a parser is easy::
559580

560581
group = OptionGroup(parser, "Dangerous Options",
561582
"Caution: use these options at your own risk. "
@@ -567,20 +588,73 @@ This would result in the following help output:
567588

568589
.. code-block:: text
569590
570-
usage: [options] arg1 arg2
591+
Usage: <yourscript> [options] arg1 arg2
592+
593+
Options:
594+
-h, --help show this help message and exit
595+
-v, --verbose make lots of noise [default]
596+
-q, --quiet be vewwy quiet (I'm hunting wabbits)
597+
-f FILE, --filename=FILE
598+
write output to FILE
599+
-m MODE, --mode=MODE interaction mode: novice, intermediate, or
600+
expert [default: intermediate]
601+
602+
Dangerous Options:
603+
Caution: use these options at your own risk. It is believed that some
604+
of them bite.
605+
606+
-g Group option.
607+
608+
A bit more complete example might invole using more than one group: still
609+
extendind the previous example::
610+
611+
group = OptionGroup(parser, "Dangerous Options",
612+
"Caution: use these options at your own risk. "
613+
"It is believed that some of them bite.")
614+
group.add_option("-g", action="store_true", help="Group option.")
615+
parser.add_option_group(group)
616+
617+
group = OptionGroup(parser, "Debug Options")
618+
group.add_option("-d", "--debug", action="store_true",
619+
help="Print debug information")
620+
group.add_option("-s", "--sql", action="store_true",
621+
help="Print all SQL statements executed")
622+
group.add_option("-e", action="store_true", help="Print every action done")
623+
parser.add_option_group(group)
624+
625+
that results in the following output:
626+
627+
.. code-block:: text
628+
629+
Usage: <yourscript> [options] arg1 arg2
630+
631+
Options:
632+
-h, --help show this help message and exit
633+
-v, --verbose make lots of noise [default]
634+
-q, --quiet be vewwy quiet (I'm hunting wabbits)
635+
-f FILE, --filename=FILE
636+
write output to FILE
637+
-m MODE, --mode=MODE interaction mode: novice, intermediate, or expert
638+
[default: intermediate]
639+
640+
Dangerous Options:
641+
Caution: use these options at your own risk. It is believed that some
642+
of them bite.
643+
644+
-g Group option.
645+
646+
Debug Options:
647+
-d, --debug Print debug information
648+
-s, --sql Print all SQL statements executed
649+
-e Print every action done
650+
651+
Another interesting method, in particular when working programmatically with
652+
option groups is:
571653

572-
options:
573-
-h, --help show this help message and exit
574-
-v, --verbose make lots of noise [default]
575-
-q, --quiet be vewwy quiet (I'm hunting wabbits)
576-
-fFILE, --file=FILE write output to FILE
577-
-mMODE, --mode=MODE interaction mode: one of 'novice', 'intermediate'
578-
[default], 'expert'
654+
.. method:: OptionParser.get_option_group(opt_str)
579655

580-
Dangerous Options:
581-
Caution: use of these options is at your own risk. It is believed that
582-
some of them bite.
583-
-g Group option.
656+
Return, if defined, the :class:`OptionGroup` that has the title or the long
657+
description equals to *opt_str*
584658

585659
.. _optparse-printing-version-string:
586660

@@ -652,14 +726,14 @@ Consider the first example above, where the user passes ``4x`` to an option
652726
that takes an integer::
653727

654728
$ /usr/bin/foo -n 4x
655-
usage: foo [options]
729+
Usage: foo [options]
656730

657731
foo: error: option -n: invalid integer value: '4x'
658732

659733
Or, where the user fails to pass a value at all::
660734

661735
$ /usr/bin/foo -n
662-
usage: foo [options]
736+
Usage: foo [options]
663737

664738
foo: error: -n option requires an argument
665739

@@ -1161,9 +1235,9 @@ must specify for any option using that action.
11611235

11621236
.. code-block:: text
11631237
1164-
usage: foo.py [options]
1238+
Usage: foo.py [options]
11651239
1166-
options:
1240+
Options:
11671241
-h, --help Show this help message and exit
11681242
-v Be moderately verbose
11691243
--file=FILENAME Input file to read data from
@@ -1358,7 +1432,7 @@ it resolves the situation by removing ``-n`` from the earlier option's list of
13581432
option strings. Now ``--dry-run`` is the only way for the user to activate
13591433
that option. If the user asks for help, the help message will reflect that::
13601434

1361-
options:
1435+
Options:
13621436
--dry-run do no harm
13631437
[...]
13641438
-n, --noisy be noisy
@@ -1374,7 +1448,7 @@ existing OptionParser::
13741448
At this point, the original ``-n``/``--dry-run`` option is no longer
13751449
accessible, so :mod:`optparse` removes it, leaving this help text::
13761450

1377-
options:
1451+
Options:
13781452
[...]
13791453
-n, --noisy be noisy
13801454
--dry-run new dry-run option

0 commit comments

Comments
 (0)