1

I am new with this argparse module.

import argparse
parser = argparse.ArgumentParser(description='Input & Output Files')
parser.add_argument('indir', type=str, help='Input File Path')
parser.add_argument('outdir1', type=str, help='Output File Path 1')
parser.add_argument('outdir2', type=str, help='Output File Path 2')
parser.add_argument('outdir3', type=str, help='Output File Path 3')
args = parser.parse_args()
print(args.indir)

Here is my code to get input and output. Below is how user should write to run my code including given input & output.

python3.7.4 test.py /input /output1 /output2 /output3

I would like to ask if it is possible if i can asked the user to enter the input & output one by one as the arguments have a very long path. For example maybe the user can type input first the followed by output1, output2 and output3.

qwe
  • 27
  • 4
  • The OS shell may have a way of splitting the inputs across lines. But what `argparse` uses is the `sys.argv` list provided by the shell and interpreter. This isn't like repeated calls to `input`. WIth the `fromfile_prefix_chars` mechanism, the user could provide a file with one string per line, saving a lot of 'last-minute' typing. – hpaulj Aug 10 '20 at 16:17

1 Answers1

1

It's not exactly that what you looked for, but there is a package called click (“Command Line Interface Creation Kit”). It offers the ability to prompt the user for input. Maybe thats a nicer way for very long paths.

A small example for your use case:

import click

@click.command()
@click.option('--indir', prompt='Input File Path', help='Input File Path', type = str, required = True)
@click.option('--outdir1', prompt='Output File Path 1', help='Output File Path 1', type = str, required =True)

def test(indir, outdir1):
    click.echo(indir)
    click.echo(outdir1)

if __name__ == '__main__':
    test()

In the CLI you get prompted to insert your paths step by step. You can also access the help page like in argparse.

Tom
  • 91
  • 7