You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Simulate command-line arguments for program run in IDLE #49930
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:
assignee='https://github.com/terryjreedy'closed_at=<Date2019-06-18.02:33:43.950>created_at=<Date2009-04-03.22:42:34.986>labels= ['3.8', 'expert-IDLE', 'type-feature', '3.7']
title='Simulate command-line arguments for program run in IDLE'updated_at=<Date2019-06-18.02:47:58.825>user='https://bugs.python.org/mrabarnett'
A different patch to solve the same issue.
This one uses a standard tkSimpleDialog to prompt for the command line, and follows the directives found at the top of the source (only took 8 years to implement... not so bad :) )
XXX GvR Redesign this interface (yet again) as follows:
Present a dialog box for ``Run Module''
Allow specify command line arguments in the dialog box
BreamoreBoy commented on Jul 28, 2010
BreamoreBoymannequin
Mannequin
More actions
Putting tjr and tal on nosy list cos it's IDLE. Apologies if I've got it wrong.
Allows the user to set command line arguments via a dialog box
Parsing - Maps the string which user entered into a list i.e. the same results are produced as when run via terminal/command prompt. The parsing methods returns a list, which is similar to the sys.argv, when you run script from command line.
Adds tests for the parsing method
Extends mock_idle for point 3
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.
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.
Get a command line from the user. 1a. What dialog? 1b. Retain it for as least the current session? Same for all editors? Specific to file?
Use the command line. 2a. parse with shlex or custom parser. 2b integrate in ScriptBinding.py when starting user code so code sees arguments in sys.argv.
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 running IDLE normally, with a subprocess, it would be possible to pass arguments to python itself when IDLE restarts the user process (with
shell.restart_shell()). That would be another issue. We would have to look at whether any options would disable idlelib.run and how running code supervised might change results.
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:
The shortcut key (F7) can be entered while on the shell window. As of now, any of the Run menu shortcuts can be entered, so I didn't fix that bug as part of this issue.
I'm not saving the arguments in any way.
I didn't (yet) make the change to print the command line arguments in the shell or to add it to the status bar.
I didn't make a change to get the sys.argv values from startup.
I added a single menu entry and not a cascade of different custom run options.
As I said 2017-06-20, msg296499, I expect to add more alternatives to the standard run to this menu option and dialog. (Cheryl, I should have pointed you to this post as well as the later ones.) I want to add just one new option to have only 1 new key to learn. I already cannot remember all of them and I expect I am not the only one. They need to be together on one dialog so that uses can conveniently pick multiple options when that makes sense. Running in a console with args is completely sensible. And exclusionary logic, such as 'Cannot supply arguments if no restart' should be easier on one dialog.
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 am pretty sure I prefer shift-F5 for the key. (But cannot be sure until I try it.) Easier to learn I think.
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.
A more than minimal patch has been merged. Further work will be on new issues. Remaining problems not previously discussed:
Tab focus traversal is wrong. This is generic to Query subclasses that add widgets. See bpo-37325.
When the customize box is cancelled, focus goes to the Shell, even when I add 'event.widget.focus_set/force()'. When the parent of the box was the editor, 'self.shell.text.focus_set' did not work to shift it to the Shell text.
Does anyone else have any idea how to put the focus back on the editor after cancel?
Activity
mrabarnett commentedon Apr 3, 2009
Patch idle-args.diff adds a dialog for entering command-line arguments
for a script from within IDLE itself.
ggenellina commentedon Feb 5, 2010
A different patch to solve the same issue.
This one uses a standard tkSimpleDialog to prompt for the command line, and follows the directives found at the top of the source (only took 8 years to implement... not so bad :) )
XXX GvR Redesign this interface (yet again) as follows:
Present a dialog box for ``Run Module''
Allow specify command line arguments in the dialog box
BreamoreBoy commentedon Jul 28, 2010
Putting tjr and tal on nosy list cos it's IDLE. Apologies if I've got it wrong.
SaimadhavHeblikar commentedon Mar 22, 2014
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?
terryjreedy commentedon Oct 2, 2014
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.
terryjreedy commentedon Mar 20, 2015
I closed bpo-23665 in favor of this one. Raymond's take: msg238108
rhettinger commentedon Mar 20, 2015
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.
terryjreedy commentedon Apr 16, 2015
Pending application of a patch, the following will work to only add args to sys.argv when running from an Idle editor.
terryjreedy commentedon Nov 23, 2015
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.
Get a command line from the user. 1a. What dialog? 1b. Retain it for as least the current session? Same for all editors? Specific to file?
Use the command line. 2a. parse with shlex or custom parser. 2b integrate in ScriptBinding.py when starting user code so code sees arguments in sys.argv.
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?
9 remaining items
[-]Command-line arguments when running in IDLE[/-][+]Simulate command-line arguments for program run in IDLE[/+]csabella commentedon Jun 2, 2019
I'll can work on a PR for this, unless you want to do it based on your research.
terryjreedy commentedon Jun 2, 2019
When running IDLE normally, with a subprocess, it would be possible to pass arguments to python itself when IDLE restarts the user process (with
shell.restart_shell()). That would be another issue. We would have to look at whether any options would disable idlelib.run and how running code supervised might change results.
terryjreedy commentedon Jun 2, 2019
My suspicion about posix=False was wrong.
terryjreedy commentedon Jun 2, 2019
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.
csabella commentedon Jun 3, 2019
I've added a minimal change for running a module in the editor with command line arguments.
Some notes:
rhettinger commentedon Jun 3, 2019
Thanks. Those seem reasonable for a first pass.
terryjreedy commentedon Jun 3, 2019
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'?
csabella commentedon Jun 3, 2019
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!
terryjreedy commentedon Jun 18, 2019
New changeset 201bc2d by Terry Jan Reedy (Cheryl Sabella) in branch 'master':
bpo-5680: IDLE: Customize running a module (GH-13763)
201bc2d
terryjreedy commentedon Jun 18, 2019
A more than minimal patch has been merged. Further work will be on new issues. Remaining problems not previously discussed:
Tab focus traversal is wrong. This is generic to Query subclasses that add widgets. See bpo-37325.
When the customize box is cancelled, focus goes to the Shell, even when I add 'event.widget.focus_set/force()'. When the parent of the box was the editor, 'self.shell.text.focus_set' did not work to shift it to the Shell text.
Does anyone else have any idea how to put the focus back on the editor after cancel?
miss-islington commentedon Jun 18, 2019
New changeset 81f7899 by Miss Islington (bot) in branch '3.7':
bpo-5680: IDLE: Customize running a module (GH-13763)
81f7899
miss-islington commentedon Jun 18, 2019
New changeset ae526ee by Miss Islington (bot) in branch '3.8':
bpo-5680: IDLE: Customize running a module (GH-13763)
ae526ee