Title: Command-line arguments when running in IDLE
Type: enhancement Stage: patch review
Components: IDLE Versions: Python 3.6, Python 3.5, Python 3.4, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Saimadhav.Heblikar, asvetlov, ggenellina, gpolo, markroseman, mrabarnett, rhettinger, terry.reedy, wolma
Priority: normal Keywords: patch

Created on 2009-04-03 22:42 by mrabarnett, last changed 2015-11-23 20:03 by terry.reedy.

File name Uploaded Description Edit
idle-args.diff mrabarnett, 2009-04-03 22:42
issue5680-patch2.diff ggenellina, 2010-02-05 03:57 issue5680-patch2 review
cmd-arg.patch Saimadhav.Heblikar, 2014-03-22 15:59 review
Messages (10)
msg85341 - (view) Author: Matthew Barnett (mrabarnett) * Date: 2009-04-03 22:42
Patch idle-args.diff adds a dialog for entering command-line arguments
for a script from within IDLE itself.
msg98864 - (view) Author: Gabriel Genellina (ggenellina) Date: 2010-02-05 03:57
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
msg111838 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-28 20:24
Putting tjr and tal on nosy list cos it's IDLE.  Apologies if I've got it wrong.
msg214484 - (view) Author: Saimadhav Heblikar (Saimadhav.Heblikar) * Date: 2014-03-22 15:59
Submitting a patch for 3.4.

1. Allows the user to set command line arguments via a dialog box
2. 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.
3. Adds tests for the parsing method
4. 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?
msg228269 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-10-02 22:24
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.
msg238729 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-03-20 22:04
I closed #23665 in favor of this one.  Raymond's take: msg238108
msg238734 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-03-20 22:35
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:
* Wing-IDE:
* Visual Studio:
* PyCharm:

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.
msg241206 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-04-16 08:09
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\\', 'a', '-2']
msg255209 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-11-23 18:15
Today on SO:  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.  

1) 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?

2) Use the command line.  2a. parse with shlex or custom parser. 2b integrate in 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 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?
msg255215 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-11-23 20:03
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.
Date User Action Args
2015-11-23 20:03:37terry.reedysetmessages: + msg255215
2015-11-23 18:15:13terry.reedysetversions: + Python 3.6
nosy: + markroseman

messages: + msg255209

assignee: asvetlov -> terry.reedy
type: behavior -> enhancement
2015-04-16 09:22:27wolmasetnosy: + wolma
2015-04-16 08:09:52terry.reedysetmessages: + msg241206
2015-03-20 22:35:18rhettingersetnosy: + rhettinger
messages: + msg238734
2015-03-20 22:04:35terry.reedysetmessages: + msg238729
2015-03-20 22:03:37terry.reedylinkissue23665 superseder
2014-10-02 22:24:12terry.reedysetmessages: + msg228269
versions: + Python 3.4, Python 3.5, - Python 3.1, Python 3.2
2014-03-22 15:59:08Saimadhav.Heblikarsetfiles: + cmd-arg.patch
nosy: + Saimadhav.Heblikar
messages: + msg214484

2014-02-04 11:59:42taleinatsetnosy: - taleinat
2014-02-03 19:13:11BreamoreBoysetnosy: - BreamoreBoy
2012-03-31 17:38:53asvetlovsetassignee: asvetlov
2012-03-26 19:37:32asvetlovsetnosy: + asvetlov
2010-07-28 20:24:13BreamoreBoysetnosy: + terry.reedy, taleinat, BreamoreBoy

messages: + msg111838
versions: + Python 3.2
2010-02-05 03:57:32ggenellinasetfiles: + issue5680-patch2.diff
nosy: + ggenellina
messages: + msg98864

2009-04-26 22:23:39ajaksu2setpriority: normal
nosy: + gpolo
versions: + Python 3.1

stage: patch review
2009-04-03 22:42:35mrabarnettcreate