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

Created on 2009-04-03 22:42 by mrabarnett, last changed 2015-04-16 09:22 by wolma.

Files
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 sahutd, 2014-03-22 15:59 review
Messages (8)
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 (sahutd) * 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: https://wingware.com/doc/intro/tutorial-debugging-launch
* Visual Studio: https://www.youtube.com/watch?v=IgbQCRHKV-Y
* PyCharm: https://www.jetbrains.com/pycharm/help/run-debug-configuration-python.html

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\\tem.py', 'a', '-2']
History
Date User Action Args
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:08sahutdsetfiles: + cmd-arg.patch
nosy: + sahutd
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