0

I have been designing a command line program with argparse that requires a user specify an input directory, and an output directory, and a directory where certain 3rd party executables will be stored.

After these directories are specified, the user must then specify which files they want to be analyzed and with what parameters, and then retrieve the output of said analysis in the output files. From that point onward, the user does not need to specify the input, output, or executable directories (unless they later decide to change the location of one or more of the directories).

Given this situation, I was wondering if it would be possible for the user to set the location of these directories in the command line program, and then run the program with however many input files and/or parameters they desire without needing to re-specify the location of their directories. I thought it would make the command line less cluttered, and thus make it easier for the user to use my program. Is this possible?

Bob McBobson
  • 743
  • 1
  • 9
  • 29
  • Just make a configuration file and store their settings in it - load them when the user runs your program without specifying those arguments, and if they do use the given versions. Allow the user to overwrite the settings in the file. – hnefatl Jul 20 '17 at 16:31
  • Python scripts don't maintain a state between invocations, so you have to preserve the information in a file or the system environment. – hpaulj Jul 20 '17 at 16:40
  • @hnefatl could you refer me to a resource on how to set up a configuration file within an argparse-based python command line program? – Bob McBobson Jul 20 '17 at 16:49
  • I don't use python, but I'd imagine it would involve standard file operations and just extracting the relevant data from argparse - "if directory option not specified, load setting from file. If setting doesn't exist, print error". [This](https://stackoverflow.com/questions/18862836/how-to-open-file-using-argparse) may be relevant, but really I think that you just need to look into normal file IO. – hnefatl Jul 20 '17 at 16:59
  • @hnefatl ok, I will see how best to set it up – Bob McBobson Jul 20 '17 at 17:09
  • I would look into storing the data as [json](https://docs.python.org/2/library/json.html) or as an [INI](https://docs.python.org/2/library/configparser.html). – SethMMorton Jul 21 '17 at 01:12
  • Also, RE: "how to set up a configuration file within an argparse-based python command line program": Try not to think of it that way. Your problem is how to store data on disk between different runs of your program, which has nothing to do with `argparse`. Try to decouple in your mind the argument parsing from reading data that is stored on disk. This is the first step to writing more modular programs, which will be much easier to maintain down the road. – SethMMorton Jul 21 '17 at 01:16
  • `argparse` is simply a way of parsing what your user whats to tell you via the command line. It's first and foremost a parser. Except for a few simple cases it doesn't do anything. It just sets some variable values (via the `args` Namespace). – hpaulj Jul 21 '17 at 02:02

0 Answers0