1

I'm learning to include command line parameters in my code. I've read the docs for argparse and I tried running this script from there.

#argparse_trial.py

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                const=sum, default=max,
                help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

If I run

>python argparse_trial.py 1

in the command line, I get the expected result 1
But if I run

>argparse_trial.py 1

I get

usage: arg_parse_trial.py [-h] [--sum] N [N ...]
argparse_trial.py: error: the following arguments are required: N

I checked and the only argument the code seems to receive in the second case is the filename itself, no matter how many arguments are given.
I'm on a Windows machine and python is in my path.

Why is the second case failing in this script? How can I get it to work?

spectras
  • 13,105
  • 2
  • 31
  • 53
SvbZ3r0
  • 638
  • 4
  • 19

4 Answers4

2

This is a guess, I cannot test right now, but I believe this is what is happening:

  1. you type the name of your python file.
  2. Windows fails to run the file as a program, so…
  3. Windows tries to open the file, using the associated program (kindof with using start).
  4. While doing so, it simply ignores other things on the line, and…
  5. …generates a basic command line for the python interpreter to use, featuring just the interpreter itself and the target file to open.

Think of what happens when you "run" a text document.

If the command line is not generated correctly, running this command should fix it (replace the path as appropriate):

ftype Python.File=C:\Path\to\python.exe "%1" %*

Notice the %* at the end. If it's amiss, arguments will be dropped.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • Thanks for the answer. But I run `youtube-dl` on a regular basis. I've never included python before it and it accepts a lot of command-line arguments – SvbZ3r0 Jun 09 '16 at 05:11
  • `youtube-dl` is a regular executable (`youtube-dl.exe`), it will not follow this process. – spectras Jun 09 '16 at 05:12
  • what about `virtualenv` ? how does that work. – bhansa Jun 09 '16 at 05:15
  • No. I don't have the executable. I installed it with pip. Its a .py file. I'm going through its code right now to see if I can understand anything but it looks very complicated for my level. – SvbZ3r0 Jun 09 '16 at 05:15
  • 1
    Can you check you python file association then? It should look like `D:\Program Files\Python\python.exe "%1" %*`. If the `%*` is missing, it could cause arguments to be dropped. – spectras Jun 09 '16 at 05:16
  • @Bhansa Can you elaborate? – SvbZ3r0 Jun 09 '16 at 05:16
  • @spectras What do mean python file association? – SvbZ3r0 Jun 09 '16 at 05:17
  • 1
    @GughanRavikumar> the [settings](http://windows.microsoft.com/en-us/windows/change-file-open-program) that tell windows how to open a `.py` file. – spectras Jun 09 '16 at 05:18
  • @spectras yes, that's correct. I actually tried to set the default program as sublime and it just opened the file the editor when i typed the `program name.py` in command line. – bhansa Jun 09 '16 at 05:22
  • @spectras That seems to be the problem. Mine is set to `python.exe`. How do I change it to `python.exe "1" %*`? I can only choose `.exe` files. I can't add the `"1" %*` anywhere – SvbZ3r0 Jun 09 '16 at 05:25
  • I don't have a windows box, but I suppose there should be an option to edit the command manually. I just found this relevant question: http://superuser.com/questions/136133/how-do-i-set-advanced-file-associations-in-windows-7 – spectras Jun 09 '16 at 05:27
  • @GughanRavikumar> can you run `assoc | more` and find out the name used for python files? If so, it's possible to set the association from the command line with that information. – spectras Jun 09 '16 at 05:36
  • @spectras thank you. That solved my problem – SvbZ3r0 Jun 09 '16 at 05:37
  • @spectras @Bhansa My problem is solved, but just to know.. Can you think of a reason why `youtube-dl` worked properly even before I did this? – SvbZ3r0 Jun 09 '16 at 05:43
  • 1
    yes, because these are all exe files and will work if you have path provided in the environment variable. – bhansa Jun 09 '16 at 05:44
  • @Bhansa I do not have the .exe version. I installed it using pip. It is a .py file. – SvbZ3r0 Jun 09 '16 at 05:46
  • 2
    check C:/Python27/Scripts and see. – bhansa Jun 09 '16 at 05:46
  • @Bhansa My bad, then. Thank you for your time. – SvbZ3r0 Jun 09 '16 at 05:49
  • @GughanRavikumar Good Luck – bhansa Jun 09 '16 at 05:50
2

I think the code is working fine. You can execute the program by just choosing the default program to be python.exe for python files.
In your case python.exe "1" %* as @spectras suggested, so it will be able take command line arguments.

Also provide the path environment variable.
Programs like pip,virtualenv and youtube-dl are all executable files and if have already set the environment variable, we use it anywhere.

How to choose default program on windows:
http://windows.microsoft.com/en-in/windows/change-default-programs#1TC=windows-7

bhansa
  • 7,282
  • 3
  • 30
  • 55
  • My default program is `python.exe`. It has to be `python.exe "1" %*`, thanks to @spectra. Just change that, I'll make yours the accepted answer – SvbZ3r0 Jun 09 '16 at 05:36
0

You can Use sys library included in python and use argv() function to perform various command argunment.

find this video tutorial intuitive to understand how to!

https://www.youtube.com/watch?v=rLG7Tz6db0w

amitnair92
  • 487
  • 7
  • 20
0

This is in addition to spectras' and Bhansa's answer.
For Windows 10 user's, setting advanced file associations is a bit complicated. You'll have to edit your registry files. Run regedit.
Go to HKEY_CURRENT_USER\SOFTWARE\Classes\py_auto_file\shell\open\command and change the value of Data from
"C:\Path\To\Python\python.exe" "1"
to
"C:\Path\To\Python\python.exe" "1" %*

This might be applicable to previous versions of Windows also.

SvbZ3r0
  • 638
  • 4
  • 19
  • 1
    The configuration is FUBAR'd if .py files are using the automatically generated `py_auto_file` ProgId. Make sure that .py files are associated with `Python.File`. I would delete the `py_auto_file` entry and use Explorer's "open with" or the settings app to select the correct "Python" ProgId. If the command-line template is wrong, use regedit to modify `Python.File` in the user's `HKCU` (for an all-users installation, you can use cmd's `assoc` and `ftype` to modify the `HKLM` values). If Python 3 is installed, use the fully qualified path to the "py.exe" laucher (try `where py.exe` in cmd). – Eryk Sun Jun 09 '16 at 06:39
  • @eryksun I'm sorry, I didn't understand most of what you said. I've explained what I did to get it working on my system. Using explorer's `open with` you can't set advanced options, like `%*` So I had to edit the registry to get it working – SvbZ3r0 Jun 09 '16 at 07:13