1

Recently, I came across the use of 'argparse' library in python. It was used to get the location of input and output directories. What is the need of using 'argparse', why can't we just use input() to get the location of directories or any user input in the command line?

I tried getting the user input using input() in the command line, and it worked. I read the answer to 'What's the difference between stdin and sys.argv in python?' and it did not help resolve my doubt of when to use input() and when to use argparse.

  • 6
    They do completely different things. `argparse` is a way of parsing arguments passed into the script when it is started. `input` is a way of reading from stdin while the script is running. – khelwood Mar 30 '22 at 19:02
  • Thank you so much, I do not have a background in CS and am not able to understand how parsing differs from reading from stdin. Also, when is one preferred over the other? – Yashika Jain Mar 30 '22 at 19:10
  • 1
    If you start your program with e.g. `./myscript.py 23 fortytwo` you need a way to get the parameters `23` and `fortytwo` in your program. `argparse` helps you to do that. – Matthias Mar 30 '22 at 19:14
  • @Matthias So, in a way, argparse helps in initializing the inputs at the start of program, while through stdin, we will first have to run the program without parameters and then the user would be prompted to enter the parameters. Right? Also, apart from this, is there any other difference between the two? Thank you. – Yashika Jain Mar 30 '22 at 19:24
  • 1
    When you call a script with something like 'python myscript.py --foo bar test', the shell and Python create a list, `sys.argv` that looks like `['myscript.py', '--foo', 'bar', 'test']`. `argparse` (and other commandline parsers) help you extract information from that list. It's not the only way, but it is a convenient one that follows common programing practice. – hpaulj Mar 30 '22 at 19:27
  • `argparse` is a module, `input` is a function. – Matthias Mar 30 '22 at 19:27
  • 1
    Voting to reopen. OP is asking why `argparse` might be more useful than `input()`, not what the difference is between the two. This question is rather important for UI design. – luther Mar 30 '22 at 19:42
  • 1
    What operating system and user interface are you using? In linux we often start scripts or programs from a terminal, using an expression like `python myscript.py` or `ls` etc. If you've ever done `ls -h` to get the help, you are using commandline input. `ls` does not prompt you for `inputs`, it reads them from the commandline. Using `input` can be convenient for instructional code, but is not widely used in useful scripts. More advanced scripts will use some sort of graphical user interface. – hpaulj Mar 30 '22 at 20:06
  • 2
    If you use the commandline, your user can call the script repeatedly with the same inputs without typing anything new. If you use `input`, the user has to type the input values fresh each time he runs the script. That quickly becomes tedious, especially if there are many inputs. – hpaulj Mar 30 '22 at 20:12

1 Answers1

4

Imagine a program that needs an input path in order to operate. Let's examine how using this might look with argparse and input.

First, argparse

#! /usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("inputfile")
args = parser.parse_args()
print(args.inputfile)

You might use it like this:

$ ./myscript.py /path/to/file.txt
/path/to/file.txt

If I wasn't 100% sure of the path, I could use my shell's tab-completion to help me out

$ ./myscript.py /path/<TAB>
(selects the "to" directory)
$ ./myscript.py /path/to/<TAB>
(selects the "file.txt" file)
$ ./myscript.py /path/to/file.txt
/path/to/file.txt

That was helpful!! If I want to execute this again, I can just press the "up" arrow and it's right there on my command prompt to be executed again!


Now, let's look at using input

#! /usr/bin/env python3

inputfile = input("Inputfile? ")
print(inputfile)

You might use it like this

$ ./myscript.py
Inputfile? /path/to/file.txt

What if I didn't know the path? Could I use tab completion?

$ ./myscript.py
Inputfile? /path/<TAB>

Oh no! It just inserted a tab into the filename - I get no help.

What if I want to execute the same command again? I press up, and then have to re-type the file path before running. Imagine you are debugging this script and need to run it over and over again - re-typing the path will get old very fast.


input is really only useful if you want to force a script to be interactive, like if you want to say "Are you sure you want to proceed? [y/N]". Otherwise, it's best to accept runtime parameters on the command line to take full advantage of what the shell has to offer as well as make it so that re-execution (and using your script in another script) is much simpler.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • 1
    Perhaps useful: If you add `import readline` and `readline.parse_and_bind("tab: complete")` to `myscript.py`, you'll get simple filename completion. in `input()`. (That's what the second line is for. With just the first line you get history --within the session-- but no tab completion.) (Although I completely agree that it's usually better to use the command-line. But if you're going to ask the user for input, you might as well use readline since it's so easy.) – rici Mar 30 '22 at 21:27
  • 1
    While I wouldn't use `input` for it, it is *very* common for (Unix) tools to receive runtime data via stdin. Plus, piping from/to files can arguably make stdin easier for repeated execution than fiddling through the shell buffer. – MisterMiyagi Mar 30 '22 at 21:49
  • 1
    @MisterMiyagi Tools set up to use a pipe would read from `sys.stdin`, not `input()`, since `input()` only accepts a single line of input. That's a whole different can of worms that was outside the scope of this question. – SethMMorton Mar 30 '22 at 21:54