Title: Modify -c command-line option to accept multiple inputs
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
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))'

That would also make it possible input an indented block:

    $ python -c 'with open("somefile.txt") as f:'  '  s =' '  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)'                       

 $ 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 

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

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

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

import sys

python3 - << EOF
import sys

print("Here's another way to do it, without restrictions on quotes")
$ ./ 
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 =\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.
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