pydu/docs/cmd.rst

46 lines
1.4 KiB
ReStructuredText
Raw Normal View History

2017-11-10 14:28:44 +00:00
Cmd
-------
2017-12-14 11:55:37 +00:00
.. py:function:: pydu.cmd.run(cmd, wait=True, shell=True)
2017-11-10 14:28:44 +00:00
2017-12-14 11:55:37 +00:00
Run cmd based on ``subprocess.Popen``.
If ``wait`` is True, ``run`` will return the tuple of ``(returncode, stdout)``.
Note, ``stderr`` is redirected to ``stdout``. If ``wait`` is False, ``run``
2017-11-10 14:28:44 +00:00
will return object of ``Popen``.
``shell`` is same to parameter of ``Popen``.
2017-12-14 11:55:37 +00:00
>>> from pydu.cmd import run
>>> run('echo hello')
2017-11-10 14:28:44 +00:00
(0, b'hello\r\n') # Python 3
2017-12-14 11:55:37 +00:00
>>> run('echo hello', wait=False)
2017-11-10 14:28:44 +00:00
<subprocess.Popen at 0x22e4010f9e8>
2017-11-11 12:23:19 +00:00
2017-11-12 18:25:30 +00:00
.. py:function:: pydu.cmd.cmdline_argv()
2017-11-11 12:23:19 +00:00
Get command line argv of self python process. On Windows when using Python 2,
``cmdline_argv`` is implemented by using ``shell32.GetCommandLineArgvW`` to get
sys.argv as a list of Unicode strings. On other system or using Python 3,
``cmdline_argv`` is same to ``sys.argv``.
There is an example on PyCharm Python Console:
>>> from pydu.cmd import cmdline_argv
>>> cmdline_argv()
['/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py', '61253', '61254']
2017-12-22 11:05:21 +00:00
2017-12-22 11:15:02 +00:00
.. py:function:: pydu.cmd.chcp(code)
2017-12-22 11:05:21 +00:00
Context manager which sets the active code page number.
It could also be used as function.
>>> from pydu.cmd import chcp
>>> chcp(437)
<ctive code page number: 437>
>>> with chcp(437):
... pass
>>>
.. note:: ``chcp`` can only be used on ``Windows`` system.