129129 print(f'{name} won {prize}€!')
130130
131131 if __name__ == '__main__':
132- CLI()
132+ CLI(command )
133133
134134Note that the ``name `` and ``prize `` parameters have type hints and are
135135described in the docstring. These are shown in the help of the command line
@@ -152,11 +152,6 @@ tool. In a shell you could see the help and run a command as follows:
152152 shown, jsonargparse needs to be installed with the ``signatures `` extras
153153 require as explained in section :ref: `installation `.
154154
155- :func: `.CLI ` without arguments searches for functions and classes defined in the
156- same module and in the local context where :func: `.CLI ` is called. Giving a
157- single or a list of functions/classes as first argument to :func: `.CLI ` skips
158- the automatic search and only includes what is given.
159-
160155When :func: `.CLI ` receives a single class, the first arguments are for
161156parameters to instantiate the class, then a method name is expected (i.e.
162157methods become :ref: `sub-commands `) and the remaining arguments are for
@@ -203,6 +198,37 @@ Then in a shell you could run:
203198 >>> CLI(Main, args = [' --max_prize=1000' , ' person' , ' Lucky' ]) # doctest: +ELLIPSIS
204199 'Lucky won ...€!'
205200
201+ If the class given does not have any methods, there will be no sub-commands and
202+ :func: `.CLI ` will return an instance of the class. For example:
203+
204+ .. testcode ::
205+
206+ from dataclasses import dataclass
207+ from jsonargparse import CLI
208+
209+ @dataclass
210+ class Settings:
211+ name: str
212+ prize: int = 100
213+
214+ if __name__ == '__main__':
215+ print(CLI(Settings, as_positional=False))
216+
217+ Then in a shell you could run:
218+
219+ .. code-block :: bash
220+
221+ $ python example.py --name=Lucky
222+ Settings(name=' Lucky' , prize=100)
223+
224+ .. doctest :: :hide:
225+
226+ >>> CLI(Settings, as_positional = False , args = [' --name=Lucky' ]) # doctest: +ELLIPSIS
227+ Settings(name='Lucky', prize=100)
228+
229+ Note the use of ``as_positional=False `` to make required arguments as
230+ non-positional.
231+
206232If more than one function is given to :func: `.CLI `, then any of them can be run
207233via :ref: `sub-commands ` similar to the single class example above, i.e.
208234``example.py function [arguments] `` where ``function `` is the name of the
@@ -214,7 +240,7 @@ class and the second the name of the method, i.e. ``example.py class
214240
215241.. note ::
216242
217- The two examples above are extremely simple, only defining parameters with
243+ The examples above are extremely simple, only defining parameters with
218244 ``str `` and ``int `` type hints. The true power of jsonargparse is its
219245 support for a wide range of types, see :ref: `type-hints `. It is even
220246 possible to use general classes as type hints, allowing to easily implement
0 commit comments