Skip to content

Commit 283ebc7

Browse files
committed
Added to readme explanation of how to append to a list of classes.
1 parent 42e9c0a commit 283ebc7

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

README.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ can be achieved by adding ``+`` as suffix to the argument key, for example:
749749
.. testsetup:: append
750750

751751
parser = ArgumentParser()
752+
class MyBaseClass: pass
752753

753754
.. doctest:: append
754755

@@ -774,9 +775,38 @@ files would first assign a list and then append to this list:
774775
- 2
775776
- 3
776777
777-
Appending works for any type for the list elements. List elements with class
778-
type is also supported and the short notation for ``init_args`` when used (see
779-
:ref:`sub-classes`), gets applied to the last element of the list.
778+
Appending works for any type for the list elements. Lists with class type
779+
elements (see :ref:`sub-classes`) are also supported. To append to the list,
780+
first append a new class by using the ``+`` suffix. Then ``init_args`` for this
781+
class are specified like if the type wasn't a list, since the arguments are
782+
applied to the last class in the list. Take for example that an argument is
783+
added to a parser as:
784+
785+
.. testcode:: append
786+
787+
parser.add_argument('--list_of_instances', type=List[MyBaseClass])
788+
789+
Thanks to the short notation, command line arguments don't require to specify
790+
``class_path`` and ``init_args``. Thus, multiple classes can be appended and its
791+
arguments set as follows:
792+
793+
.. code-block:: bash
794+
795+
python tool.py \
796+
--list_of_instances+={CLASS_1_PATH} \
797+
--list_of_instances.{CLASS_1_ARG_1}=... \
798+
--list_of_instances.{CLASS_1_ARG_2}=... \
799+
--list_of_instances+={CLASS_2_PATH} \
800+
--list_of_instances.{CLASS_2_ARG_1}=... \
801+
...
802+
--list_of_instances+={CLASS_N_PATH} \
803+
--list_of_instances.{CLASS_N_ARG_1}=... \
804+
...
805+
806+
Once a new class has been appended to the list, it is not possible to modify the
807+
arguments of a previous class. This limitation is by intention since it forces
808+
classes and its arguments to be defined in order, making the command line call
809+
intuitive to write and understand.
780810

781811

782812
.. _dict-items:

0 commit comments

Comments
 (0)