-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simulate command-line arguments for program run in IDLE #49930
Comments
Patch idle-args.diff adds a dialog for entering command-line arguments |
A different patch to solve the same issue. XXX GvR Redesign this interface (yet again) as follows:
|
Putting tjr and tal on nosy list cos it's IDLE. Apologies if I've got it wrong. |
Submitting a patch for 3.4.
I am on Debian and don't have access to a windows machine. please try to run the tests on windows machine and see if its OK.(cmd handles arguments rather differently.) Also, should we persist the command line argument that user entered across sessions? |
I have a recent idea that there should be a separate and persistent execution process for each file run. This would make it easy to persist the cmd lines args for a particular module. |
Here's my comment from the duplicate tracker item: A number of IDEs support menu options to set the execution environment for programs under development and testing. In particular, it would be nice if IDLE let the user set command line arguments to be passed into sys.argv when running a script by pressing F5. Here are some existing implementations for reference:
This feature will help users interactively develop and test command-line tools while retaining all the nice features of the IDE. I would personally find it useful when teaching students about how sys.argv works. I suggest adding an option under the Run menu for Set Command Line arguments. It would trigger a dialog box that lets a user set a string such as "somefile.txt -r 10". The user string would be run through shlex to break it into separate fields. When F5 is pressed, the sys.argv list would be repopulated to include the script name and the lexed arguments. A little more elaborate option is to add a Run menu entry for Set Execution Enviroment that let's the user 1) set the command-line 2) specify the start-up directory (using os.chdir), 3) and edit the environment variables (from os.environ) or at least be able to set PYTHONPATH. |
Pending application of a patch, the following will work to only add args to sys.argv when running from an Idle editor. import sys
# ...
if __name__ == '__main__':
if 'idlelib.PyShell' in sys.modules:
sys.argv.extend(('a', '-2')) # add your argments here.
print(sys.argv) # in use, parse sys.argv after extending it
# ['C:\\Programs\\python34\\tem.py', 'a', '-2'] |
Today on SO: https://stackoverflow.com/questions/33866724/read-data-from-file-idle. Person writing script to open a file given on command line asks 'How to test from IDLE?' He found PyCharm, but I would like to add a better answer than the workaround I gave above. It does not have to be either perfect or necessarily permanent. There are two subissues.
Looking *briefly* at the patches: M.B.: Get command line with new extension module argumentDialog.py. Parse with re.findall(arg_regex). Modifies ScriptBinding, including the deletin of a condition for setting user sys.argv. G.G.: Get command line with tkSimpleDialog._QueryString. Split with shlex. Reuses MB's ScriptBinding patch. S.H.: Get command line with custom dialog. Use custom parser. Add new tests. My inclination is to start as simple as possible. Use a message box to get a single string (no error checking is needed). Set for specific file only, not all. The setting could be added to status bar. Parse with shlex. Question: is basic command line parsing system specific? Does MAC stick with unix/posix rules? Should shlex.split use non-posix mode on Windows? |
When file is run with command line args, it might be nice to add an additional separator line with the line entered. That way, if someone ran a series of experiments with different command lines, they would have the command line and output together as documentation that can be saved. |
I closed bpo-28889, with a 4th patch, in favor of this one. Gabriel, you somehow never signed the PSF Contributor License Agreement. Until you do, I will assume that anything you did is covered by the other 3 patches. |
Running with arguments is only one requested or possible deviation from the standard F5 mode. See bpo-19042, msg296498, for many more. I am thinking that there should be one menu entry (Custom Run, Alt-F5)? that brings up a box with alternatives, initially defaulting to the F5 values, then persisting. The order of execution should be a) Syntax check by compile, b) Run options, if requested, c) Restart Shell, if requested, d) Execute code object. I want to start with setting sys.args, but with a mind to adding other things. Running without explicit saving might be next. |
I've created a simple work-around for those who don't want to wait for the next release(s) of IDLE. It's available on SO: |
Can be bump this up in priority? The patch has been awaiting review for a good while. |
3 recent 'doit' requests (2 on PR 1589) raise my priority for this issue. When a user runs a file from an editor, IDLE simulates, as well as it can, the user entering 'python -i path' in a system command line shell. Both on a command line and in IDLE, the program starts with sys.argv = ['path'] (at least on my Windows system). This can be shown by running a file with 'import sys; print(sys.path)' both ways. The request for this issue is to simulate 'python -i path a1, a2, ...'. I have looked at least a bit at all 4 patches and have decided to start fresh, using just a few existing lines. The latest of the 4 was submitted 2 years ago, while I was learning to use git. By the following September, using config-extensions was obsolete. *If* we want to save a command string across sessions, a line could be added to config-main. But I am dubious about this. I expect that users will want to vary argument values for 1 program and need different sets of arguments for different programs. I want to use a subclass of idlelib.query.Query for the query box. No need to reinvent it. I believe that argument strings should be parsed into an argument list using shlex.split. No need to reinvent that either. But I am not an expert in this area and the doc it rather vague. I suspect that 'posix=False' should be used on Windows. Does anyone know? Gabriel's patch is the only one using shlex.split, but always with the default 'posix=True'. Saimadhav tested his patch on Debian. The others did not say. A verified-by-human htest should be added to runscript.py, and manual test running 'import sys; print(sys.argv)' from an editor done on the 3 major OSes. |
I'll can work on a PR for this, unless you want to do it based on your research. |
When running IDLE normally, with a subprocess, it would be possible to pass arguments to python itself when IDLE restarts the user process (with |
My suspicion about posix=False was wrong. >>> shlex.split(''' 1 "'a' 'b'" ''', posix=False)
['1', '"\'a\' \'b\'"'] # len(...[1]) = 9
>>> shlex.split(''' 1 '"a" "b"' ''')
['1', '"a" "b"'] # len = 7
f:\dev\3x>py -c "import sys; print(sys.argv)" 1 "'a' 'b'"
['-c', '1', "'a' 'b'"] # Matches 'True'
>>> shlex.split(''' 1 '"a" "b"' ''', posix=False)
['1', '\'"a" "b"\'']
>>> shlex.split(''' 1 '"a" "b"' ''')
['1', '"a" "b"'] # Similar difference
f:\dev\3x>py -c "import sys; print(sys.argv)" 1 '"a" "b"'
['-c', '1', "'a", "b'"] # crazy inversion and 3 args, matches neither |
Cheryl, go ahead after reading the notes above. Use an f-string for the runcommand argument and remember that it is run within the pristine user environment. Otherwise, let's start with minimal changes. |
I've added a minimal change for running a module in the editor with command line arguments. Some notes:
|
Thanks. Those seem reasonable for a first pass. |
Two design issues:
The configuration and pseudoevent names, perhaps 'run-custom', should be changed to reflect that now. For the menu, I am less sure. Perhaps 'Run Custom', 'Run Setup', 'Run with Alternatives', 'Alternate Runs'?
|
I've pushed a change for a more generic dialog for a 'Run Custom' option and I changed the keybinding to Shift-F5. Sorry for not doing that on the initial commit; I didn't fully understand the difference between what you were asking for and the minimal version I had committed. For this change, I also added a 'Restart shell' checkbox to the dialog, more as a proof-of-concept than anything else (although I did hook it up, so it should be functional). Is this the type of dialog you had in mind? I made some comments on the PR as to some of the decisions I've run into so far with the design. Thanks! |
A more than minimal patch has been merged. Further work will be on new issues. Remaining problems not previously discussed:
Does anyone else have any idea how to put the focus back on the editor after cancel? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: