Debian Codegraph @

doc/task-args.rst

raw
1 .. meta::
2 :description: Parametrizing tasks with command line arguments and environment variables.
3 :keywords: python, doit, documentation, guide, parametrization, task_param
4
5 .. title:: Task parametrization, getting arguments from command line
6
7
8 Passing arguments from the command line
9 =======================================
10
11 .. _parameters:
12
13
14 Task action parameters
15 ----------------------
16
17 It is possible to pass option parameters to the task action through the command line.
18
19 Just add a ``params`` field to the task dictionary. ``params`` must be a list of
20 dictionaries where every entry is an option parameter. Each parameter must
21 define a name, and a default value. It can optionally define a "short" and
22 "long" names to be used from the command line (it follows unix command line
23 conventions). It may also specify additional attributes, such as
24 `type` and `help` (see :ref:`below <parameters-attributes>`).
25
26
27 See the example:
28
29 .. literalinclude:: samples/parameters.py
30
31
32 For python-actions the python function must define arguments with the same name as a task parameter.
33
34 .. code-block:: console
35
36 $ doit py_params -p abc --param2 4
37 . py_params
38 abc
39 9
40
41 Need a list in your python function? Specify an option with ``type``
42 set to ``list``.
43
44 .. code-block:: console
45
46 $ doit py_params_list -l milk -l eggs -l bread
47 . py_params_list
48 milk
49 eggs
50 bread
51
52 Choices can be set by specifying an option with ``choices`` set to a
53 sequence of a 2-element tuple.
54 The first element is the choice value.
55 The second element is the choice description,
56 if not required, use an empty string.
57
58 .. code-block:: console
59
60 $ doit py_params_choice -c that
61 . py_params_choice
62 that
63
64 Invalid choices are detected and passed back to the user.
65
66 .. code-block:: console
67
68 $ doit py_params_choice -c notavalidchoice
69 ERROR: Error parsing parameter 'choice'. Provided 'notavalidchoice' but available choices are: 'this', 'that'.
70
71
72 For cmd-actions use python string substitution notation:
73
74 .. code-block:: console
75
76 $ doit cmd_params -f "-c --other value"
77 . cmd_params
78 mycmd -c --other value xxx
79
80 .. _parameters-attributes:
81
82 All parameters attributes
83 ^^^^^^^^^^^^^^^^^^^^^^^^^
84
85 Here is the list of all attributes ``param`` accepts:
86
87 ``name``
88 Name of the parameter, identifier used as name of the the parameter
89 on python code.
90 It should be unique among others.
91
92 :required: True
93 :type: `str`
94
95 ``default``
96 Default value used when it is set through command-line.
97
98 :required: True
99
100 ``short``
101 Short parameter form, used for e.g. ``-p value``.
102
103 :required: optional
104 :type: `str`
105
106 ``long``
107 Long parameter form, used for e.g. ``--parameter value``.
108
109 :required: optional
110 :type: `str`
111
112 ``type``
113 Actually it can be any python callable.
114 It coverts the string value received from command line to whatever
115 value to be used on python code.
116
117 If the ``type`` is ``bool`` the parameter is treated as an *option flag*
118 where no value should be specified, value is set to ``True``.
119 Example: ``doit mytask --flag``.
120
121 :required: optional
122 :type: `callable` (e.g. a `function`)
123 :default: `str`
124
125 ``choices``
126 List of accepted value choices for option.
127 First tuple element is the value name,
128 second tuple element is a help description for value.
129
130 :required: optional
131 :type: list of 2-tuple strings
132
133 ``help``
134 Help message associated to this parameter, shown when
135 :ref:`help <cmd-help>` is called for this task,
136 e.g. ``doit help mytask``.
137
138 :required: optional
139 :type: `str`
140
141 ``inverse``
142 [only for `bool` parameter]
143 Set inverse flag long parameter name, value will be set to ``False``
144 (see example below).
145
146 :required: optional
147 :type: `str`
148
149 Example, given following code:
150
151 .. literalinclude:: samples/parameters_inverse.py
152
153 calls to task `with_flag` show flag on or off:
154
155 .. code-block:: console
156
157 $ doit with_flag
158 . with_flag
159 Flag On
160 $ doit with_flag --flagoff
161 . with_flag
162 Flag Off
163
164
165 positional arguments
166 --------------------
167
168 Tasks might also get positional arguments from the command line
169 as standard unix commands do,
170 with positional arguments *after* optional arguments.
171
172 .. literalinclude:: samples/pos.py
173
174 .. code-block:: console
175
176 $ doit pos_args -p 4 foo bar
177 . pos_args
178 param1 is: 4
179 positional-0: foo
180 positional-1: bar
181
182
183 .. warning::
184
185 If a task accepts positional arguments, it is not allowed to pass
186 other tasks after it in the command line. For example if `task1`
187 takes positional arguments you can not call::
188
189 $ doit task1 pos1 task2
190
191 As the string `task2` would be interpreted as positional argument from
192 `task1` not as another task name.
193
194
195
196
197 .. _command line variables:
198
199 command line variables (*doit.get_var*)
200 -----------------------------------------
201
202 It is possible to pass variable values to be used in dodo.py from the command line.
203
204 .. literalinclude:: samples/get_var.py
205
206 .. code-block:: console
207
208 $ doit
209 . echo
210 hi {abc: NO}
211 $ doit abc=xyz x=3
212 . echo
213 hi {abc: xyz}
214
215
216 Task creator parameters
217 -----------------------
218
219 Command line arguments may also be passed to task creators.
220 It uses the same parameter syntax as is used with task action parameters.
221
222 .. code-block:: python
223
224 from doit import task_params
225
226 @task_params([{"name": "howmany", "default": 3, "type": int, "long": "howmany"}])
227 def task_subtasks(howmany):
228 for i in range(howmany):
229 yield {"name": i, "actions": [f"echo I can count to {howmany}: {i}"]}
230
231
232 Any argument defined for the task generating function will also be available as an
233 argument for any task actions.
234
235 .. code-block:: python
236
237 def do_work(foo):
238 print(f'Argument foo={foo}')
239
240 @task_params([{"name": "foo", "default": "bar", "long": "foo"}])
241 def task_use_in_action(foo):
242 print(f'When the task action runs it will print {foo}')
243
244 return {
245 'actions': [do_work],
246 'verbosity': 2
247 }
248
249
250 .. warning::
251
252 When the ``@task_params`` decorator is used, you must not use the ``params`` field.
253 The content from ``params`` can be easily included in ``@task_params``.
254
255 The reason for this limitation is that the command line parsing happens before the task's dict
256 is returned. Hence impossible to know its value.