classification
Title: Modify -c command-line option to accept multiple inputs
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, rhettinger, serhiy.storchaka, zach.ware
Priority: normal Keywords:

Created on 2020-05-08 19:08 by rhettinger, last changed 2020-05-23 16:30 by DahlitzFlorian.

Messages (4)
msg368454 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-08 19:08
It would be nice to write this:

    $ python -c 'from math import *' 'print(e**(1j * pi))'

Instead of this:

    $ python -c 'from math import *; print(e**(1j * pi))'
    (-1+1.22464679915e-16j)

That would also make it possible input an indented block:

    $ python -c 'with open("somefile.txt") as f:'  '  s = f.read()' '  print(len(s))'

This feature would be especially useful in bash scripts.

FWIW, the timeit module already supports this convenience:

    $ python -m timeit -s 'from math import sqrt' -s 'x = 5' 'y = x ** 2' 'z = sqrt(y)'
    10000000 loops, best of 3: 0.0819 usec per loop
msg368456 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python triager) Date: 2020-05-08 19:16
@rhettinger can you clarify how does this arguments act on argv? Should they be hidden like the first argument, or should they remain visible as a passing argument? 
 $ python -c 'import sys;print(sys.argv)'                       
['-c']

 $ python -c 'import sys;print(sys.argv)' 'print("another")' 'print("another1")'
['-c', 'print("another")', 'print("another1")']
msg368465 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-05-08 21:13
This can be accomplished already by just including newlines in the string:

$ cat multiline_python_bash_script.sh 
#!/bin/bash

python3 -c '
from math import *
print(e**(1j * pi))
import sys
print(sys.argv)'

echo "here's some random text" > somefile.txt

python3 -c '
with open("somefile.txt") as f:
    print(len(f.read()))

import sys
print(sys.argv)'


python3 - << EOF
import sys

print("Here's another way to do it, without restrictions on quotes")
print(sys.argv)
EOF
$ ./multiline_python_bash_script.sh 
(-1+1.2246467991473532e-16j)
['-c']
24
['-c']
Here's another way to do it, without restrictions on quotes
['-']
$ python3 -c "
> print('Interactive test:')
> print('Multi-line works')
> "
Interactive test:
Multi-line works



None of these options is particularly *pretty*, but if it's at the point the ugliness bothers you it should probably be its own file to be executed :).  On the other hand, none of these options really works on Windows, as far as I know.
msg369620 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-22 17:19
It would be breaking change.

In addition to multiline strings you can use \n with $' on Posix:

python -c $'with open("somefile.txt") as f:\n  s = f.read()\n  print(len(s))'

Maybe there is similar feature on Windows, but in any case the command line interface on Windows is far from user friendly.
History
Date User Action Args
2020-05-23 16:30:57DahlitzFloriansetnosy: - DahlitzFlorian
2020-05-22 17:19:26serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg369620
2020-05-22 10:00:56DahlitzFloriansetnosy: + DahlitzFlorian
2020-05-08 21:13:20zach.waresetnosy: + zach.ware
messages: + msg368465
2020-05-08 19:16:46BTaskayasetnosy: + BTaskaya
messages: + msg368456
2020-05-08 19:08:57rhettingercreate