Teks sumber — English
Terjemahan — Bahasa Indonesia
However, the ``argparse`` version will also handle some parameter combination differently from the way the ``optparse`` version would handle them. For example (amongst other differences):
With these few lines of code, users of your script can now do the "usual thing" on the command-line, for example::
For example, consider this hypothetical command-line::
Actions tell :mod:`!optparse` what to do when it encounters an option on the command line. There is a fixed set of actions hard-coded into :mod:`!optparse`; adding new actions is an advanced topic covered in section :ref:`optparse-extending-optparse`. Most actions tell :mod:`!optparse` to store a value in some variable---for example, take a string from the command line and store it in an attribute of ``options``.
For example::
Sebagai contoh::
Flag options---set a variable to true or false when a particular option is seen---are quite common. :mod:`!optparse` supports them with two separate actions, ``store_true`` and ``store_false``. For example, you might have a ``verbose`` flag that is turned on with ``-v`` and off with ``-q``::
Here, "MODE" is called the meta-variable: it stands for the argument that the user is expected to supply to ``-m``/``--mode``. By default, :mod:`!optparse` converts the destination variable name to uppercase and uses that for the meta-variable. Sometimes, that's not what you want---for example, the ``--filename`` option explicitly sets ``metavar="FILE"``, resulting in this automatically generated option description::
For example, if your script is called ``/usr/bin/foo``:
For example, when you call ::
Set parsing to stop on the first non-option. For example, if ``-a`` and ``-b`` are both simple options that take no arguments, :mod:`!optparse` normally accepts this syntax::
Use this if you have a command processor which runs another command which has options of its own and you want to make sure these options don't get confused. For example, each command might have a different set of options.
Set default values for several option destinations at once. Using :meth:`set_defaults` is the preferred way to set default values for options, since multiple options can share the same destination. For example, if several "mode" options all set the same destination, any one of them can set the default, and the last one wins::
Of course, you could put any condition in there---you're not limited to checking the values of already-defined options. For example, if you have options that should not be called when the moon is full, all you have to do is this::
For example, let's add an ``"extend"`` action. This is similar to the standard ``"append"`` action, but instead of taking a single value from the command-line and appending it to an existing list, ``"extend"`` will take multiple values in a single comma-delimited string, and extend an existing list with them. That is, if ``--names`` is an ``"extend"`` option of type ``"string"``, the command line ::