File: special_arguments.rst

package info (click to toggle)
python-sh 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: python: 4,157; makefile: 25
file content (637 lines) | stat: -rw-r--r-- 15,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
.. _special_arguments:

.. |def| replace:: Default value:

Special Kwargs
##############

These arguments alter a command's behavior.  They are not passed to the program.
You can use them on any command that you run, but some may not be used together.
sh will tell you if there are conflicts.

To set default special keyword arguments on *every* command run, you may use
:ref:`default_arguments`.

Controlling Output
==================

.. _out:

_out
----
|def| ``None``

What to redirect STDOUT to.  If this is a string, it will be treated as a file
name.  You may also pass a file object (or file-like object), an int
(representing a file descriptor, like the result of :func:`os.pipe`), a
:class:`io.StringIO` object, or a callable.

.. code-block:: python

    import sh
    sh.ls(_out="/tmp/output")

.. seealso::
    :ref:`redirection`
		
.. _err:

_err
----
|def| ``None``

What to redirect STDERR to.  See :ref:`_out<out>`.
    
_err_to_out
-----------
|def| ``False``

If ``True``, duplicate the file descriptor bound to the process's STDOUT also to
STDERR, effectively causing STDERR and STDOUT to go to the same place.

_encoding
---------
|def| ``sh.DEFAULT_ENCODING``

The character encoding of the process's STDOUT.  By default, this is the
locale's default encoding.
			
_decode_errors
--------------
.. versionadded:: 1.07.0

|def| ``"strict"``

This is how Python should handle decoding errors of the process's output.
By default, this is ``"strict"``, but you can use any value that's valid
to :meth:`bytes.decode`, such as ``"ignore"``.
		
_tee
----
.. versionadded:: 1.07.0

|def| ``None``

As of 1.07.0, any time redirection is used, either for STDOUT or STDERR, the
respective internal buffers are not filled.  For example, if you're downloading
a file and using a callback on STDOUT, the internal STDOUT buffer, nor the pipe
buffer be filled with data from STDOUT.  This option forces one of stderr
(``_tee='err'``) or stdout (``_tee='out'`` or ``_tee=True``) to be filled
anyways, in effect "tee-ing" the output into two places (the callback/redirect
handler, and the internal buffers).


_truncate_exc
-------------
.. versionadded:: 1.12.0

|def| ``True``

Whether or not exception ouput should be truncated.

Execution
=========

.. _fg:

_fg
---
.. versionadded:: 1.12.0

|def| ``False``

Runs a command in the foreground, meaning it is spawned using :func:`os.spawnle()`.  The current process's STDIN/OUT/ERR
is :func:`os.dup2`'d to the new process and so the new process becomes the *foreground* of the shell executing the
script.  This is only really useful when you want to launch a lean, interactive process that sh is having trouble
running, for example, ssh.

.. warning::

    ``_fg=True`` side-steps a lot of sh's functionality.  You will not be returned a process object and most (likely
    all) other special kwargs will not work.

If you are looking for similar functionality, but still retaining sh's features, use the following:

.. code-block:: python
        
    import sh
    import sys
    sh.your_command(_in=sys.stdin, _out=sys.stdout, _err=sys.stderr)


.. _bg:

_bg
---
|def| ``False``

Runs a command in the background.  The command will return immediately, and you
will have to run :meth:`RunningCommand.wait` on it to ensure it terminates.

.. seealso:: :ref:`background`.

.. _bg_exc:

_bg_exc
-------
.. versionadded:: 1.12.9

|def| ``True``

Automatically report exceptions for the background command. If you set this to
``False`` you should make sure to call :meth:`RunningCommand.wait` or you may
swallow exceptions that happen in the background command.

.. _async_kw:

_async
------
.. versionadded:: 2.0.0

|def| ``False``

Allows your command to become awaitable. Use in combination with :ref:`_iter <iter>`
and ``async for`` to incrementally await output as it is produced.

.. _env:

_env
----
|def| ``None``

A dictionary defining the only environment variables that will be made
accessible to the process.  If not specified, the calling process's environment
variables are used.

.. note::

    This dictionary is the authoritative environment for the process.  If you
    wish to change a single variable in your current environement, you must pass
    a copy of your current environment with the overriden variable to sh.

.. seealso:: :ref:`environments`

.. _timeout:

_timeout
--------
|def| ``None``

How much time, in seconds, we should give the process to complete.  If the
process does not finish within the timeout, it will be sent the signal defined
by :ref:`timeout_signal`.

.. _timeout_signal:

_timeout_signal
---------------
|def| ``signal.SIGKILL``

The signal to be sent to the process if :ref:`timeout` is not ``None``.

_cwd
----
|def| ``None``

A string that sets the current working directory of the process.

.. _ok_code:

_ok_code
--------
|def| ``0``

Either an integer, a list, or a tuple containing the exit code(s) that are
considered "ok", or in other words: do not raise an exception.  Some misbehaved
programs use exit codes other than 0 to indicate success.

.. code-block:: python

    import sh
    sh.weird_program(_ok_code=[0,3,5])

.. seealso:: :ref:`exit_codes`

.. _new_session:

_new_session
------------
|def| ``False``

Determines if our forked process will be executed in its own session via
:func:`os.setsid`.

.. versionchanged:: 2.0.0
    The default value of ``_new_session`` was changed from ``True`` to ``False``
    because it makes more sense for a launched process to default to being in
    the process group of python script, so that it receives SIGINTs correctly.

.. note::

    If ``_new_session`` is ``False``, the forked process will be put into its
    own group via ``os.setpgrp()``.  This way, the forked process, and all of
    it's children, are always alone in their own group that may be signalled
    directly, regardless of the value of ``_new_session``.

.. seealso:: :ref:`architecture`

.. _uid:

_uid
----
.. versionadded:: 1.12.0

|def| ``None``

The user id to assume before the child process calls :func:`os.execv`.

_preexec_fn
-----------
.. versionadded:: 1.12.0

|def| ``None``

A function to be run directly before the child process calls :func:`os.execv`.
Typically not used by normal users.

.. _pass_fds:

_pass_fds
---------
.. versionadded:: 1.13.0

|def| ``{}`` (empty set)

A whitelist iterable of integer file descriptors to be inherited by the child. Passing anything in this argument causes :ref:`_close_fds <close_fds>` to be ``True``.
		
.. _close_fds:

_close_fds
----------
.. versionadded:: 1.13.0

|def| ``True``

Causes all inherited file descriptors besides stdin, stdout, and stderr to be automatically closed. This option is
automatically enabled when :ref:`_pass_fds <pass_fds>` is given a value.

Communication
=============

.. _in:

_in
---

|def| ``None``

Specifies an argument for the process to use as its standard input.  This may be
a string, a :class:`queue.Queue`, a file-like object, or any iterable.

.. seealso:: :ref:`stdin`
		
.. _piped:

_piped
------

|def| ``None``

May be ``True``, ``"out"``, or ``"err"``.  Signals a command that it is being
used as the input to another command, so it should return its output
incrementally as it receives it, instead of aggregating it all at once.

.. seealso:: :ref:`Advanced Piping <advanced_piping>`

.. _iter:
		
_iter
-----

|def| ``None``

May be ``True``, ``"out"``, or ``"err"``.  Puts a command in iterable mode.  In
this mode, you can use a ``for`` or ``while`` loop to iterate over a command's
output in real-time.

.. code-block:: python

    import sh 
    for line in sh.cat("/tmp/file", _iter=True):
        print(line)

.. seealso:: :ref:`iterable`.

.. _iter_noblock:
		
_iter_noblock
-------------
|def| ``None``

Same as :ref:`_iter <iter>`, except the loop will not block if there is no
output to iterate over.  Instead, the output from the command will be
:py:data:`errno.EWOULDBLOCK`.

.. code-block:: python

    import sh
    import errno
    import time

    for line in sh.tail("-f", "stuff.log", _iter_noblock=True):
        if line == errno.EWOULDBLOCK:
            print("doing something else...")
            time.sleep(0.5)
        else:
            print("processing line!")


.. seealso:: :ref:`iterable`.

.. _with:

_with
-----
|def| ``False``

Explicitly tells us that we're running a command in a ``with`` context.  This is
only necessary if you're using a command in a ``with`` context **and** passing
parameters to it.

.. code-block:: python

    import sh
    with sh.contrib.sudo(password="abc123", _with=True):
        print(sh.ls("/root"))

.. seealso:: :ref:`with_contexts`

.. _done:

_done
-----
.. versionadded:: 1.11.0

|def| ``None``

A callback that is *always* called when the command completes, even if it
completes with an exit code that would raise an exception.  After the callback
is run, any exception that would be raised is raised.

The callback is passed the :ref:`RunningCommand <running_command>` instance, a
boolean indicating success, and the exit code.

.. include:: /examples/done.rst
		
TTYs
====

.. _tty_in:

_tty_in
-------

|def| ``False``, meaning a :func:`os.pipe` will be used.

If ``True``, sh creates a TTY for STDIN, essentially emulating a terminal, as if
your command was entered from the commandline.  This is necessary for commands
that require STDIN to be a TTY.

.. _tty_out:
    
_tty_out
--------

|def| ``True``

If ``True``, sh creates a TTY for STDOUT, otherwise use a :func:`os.pipe`. This
is necessary for commands that require STDOUT to be a TTY.

.. seealso:: :ref:`faq_tty_out`

.. _unify_ttys:

_unify_ttys
-----------
.. versionadded:: 1.13.0

|def| ``False``

If ``True``, sh will combine the STDOUT and STDIN TTY into a single
pseudo-terminal. This is sometimes required by picky programs which expect to be
dealing with a single pseudo-terminal, like SSH.

.. seealso:: :ref:`tutorial2`

_tty_size
---------

|def| ``(20, 80)``

The (rows, columns) of stdout's TTY.  Changing this may affect how much your
program prints per line, for example.

Performance & Optimization
==========================

_in_bufsize
-----------
|def| ``0``

The STDIN buffer size.  0 for unbuffered, 1 for line buffered, anything else for
a buffer of that amount.

.. _out_bufsize:
		
_out_bufsize
------------
|def| ``1``

The STDOUT buffer size.  0 for unbuffered, 1 for line buffered, anything
else for a buffer of that amount.

.. _err_bufsize:

_err_bufsize
------------
|def| ``1``

Same as :ref:`out_bufsize`, but with STDERR.

.. _internal_bufsize:
		
_internal_bufsize
-----------------
|def| ``3 * 1024**2`` chunks

How much of STDOUT/ERR your command will store internally.  This value
represents the *number of bufsize chunks* not the total number of bytes.  For
example, if this value is 100, and STDOUT is line buffered, you will be able to
retrieve 100 lines from STDOUT.  If STDOUT is unbuffered, you will be able to
retrieve only 100 characters.
		
_no_out
-------
.. versionadded:: 1.07.0

|def| ``False``

Disables STDOUT being internally stored.  This is useful for commands
that produce huge amounts of output that you don't need, that would
otherwise be hogging memory if stored internally by sh.
		
_no_err
-------
.. versionadded:: 1.07.0

|def| ``False``

Disables STDERR being internally stored.  This is useful for commands that
produce huge amounts of output that you don't need, that would otherwise be
hogging memory if stored internally by sh.
		
_no_pipe
--------
.. versionadded:: 1.07.0

|def| ``False``

Similar to ``_no_out``, this explicitly tells the sh command that it will never
be used for piping its output into another command, so it should not fill its
internal pipe buffer with the process's output.  This is also useful for
conserving memory.
		

Program Arguments
=================

These are options that affect how command options are fed into the program.

_long_sep
---------
.. versionadded:: 1.12.0

|def| ``"="``

This is the character(s) that separate a program's long argument's key from the
value, when using kwargs to specify your program's long arguments.  For example,
if your program expects a long argument in the form ``--name value``, the way to
achieve this would be to set ``_long_sep=" "``.

.. code-block:: python

    import sh
    sh.your_program(key=value, _long_sep=" ")

Would send the following list of arguments to your program:

.. code-block:: python

    ["--key value"]

If your program expects the long argument name to be separate from its value,
pass ``None`` into ``_long_sep`` instead:

.. code-block:: python

    import sh
    sh.your_program(key=value, _long_sep=None)

Would send the following list of arguments to your program:

.. code-block:: python

    ["--key", "value"]

_long_prefix
------------
.. versionadded:: 1.12.0

|def| ``"--"``

This is the character(s) that prefix a long argument for the program being run.
Some programs use single dashes, for example, and do not understand double
dashes.

.. _preprocess:

_arg_preprocess
---------------
.. versionadded:: 1.12.0

|def| ``None``

This is an advanced option that allows you to rewrite a command's arguments on
the fly, based on other command arguments, or some other variable.  It is really
only useful in conjunction with :ref:`baking <baking>`, and only currently used when
constructing :ref:`contrib <contrib>` wrappers.

Example:

.. code-block:: python

    import sh

    def processor(args, kwargs):
        return args, kwargs

    my_ls = sh.bake.ls(_arg_preprocess=processor)

.. warning::

    The interface to the ``_arg_preprocess`` function may change without
    warning.  It is generally only for internal sh use, so don't use it unless
    you absolutely have to.

Misc
====

_log_msg
--------

|def| ``None``

.. versionadded:: 1.12.0

This allows for a custom logging header for :ref:`command_class` instances.  For example, the default logging looks like this:

.. code-block:: python

    import logging
    import sh

    logging.basicConfig(level=logging.INFO)

    sh.ls("-l")

.. code-block:: none

    INFO:sh.command:<Command '/bin/ls -l'>: starting process
    INFO:sh.command:<Command '/bin/ls -l', pid 28952>: process started
    INFO:sh.command:<Command '/bin/ls -l', pid 28952>: process completed

People can find this ``<Command ..`` section long and not relevant. ``_log_msg`` allows you to customize this:

.. code-block:: python

    import logging
    import sh

    logging.basicConfig(level=logging.INFO)

    def custom_log(ran, call_args, pid=None):
        return ran

    sh.ls("-l", _log_msg=custom_log)

.. code-block:: none

    INFO:sh.command:/bin/ls -l: starting process
    INFO:sh.command:/bin/ls -l: process started
    INFO:sh.command:/bin/ls -l: process completed

The first argument, ``ran``, is the program's execution string and arguments, as close as we can get it to be how you'd
type in the shell.  ``call_args`` is a dictionary of all of the special kwargs that were passed to the command.  And ``pid``
is the process id of the forked process.  It defaults to ``None`` because the ``_log_msg`` callback is actually called
twice: first to construct the logger for the :ref:`running_command` instance, before the process itself is spawned, then
a second time after the process is spawned via :ref:`oproc_class`, when we have a pid.