0

basic scenario:

building an interpreter for some basic scripting language, I need to specify the file to run, so that I could, in CMD type $ customlang /path/to/file/file.lang

I've looked into argparse, but what i've seen is that it parses arguments in the form of --argument which isn't quite what I'm looking for. Any suggestions?

thanks

J-Cake
  • 1,526
  • 1
  • 15
  • 35
  • 3
    https://docs.python.org/3/library/sys.html#sys.argv – Tomalak Apr 19 '18 at 06:59
  • Thanks. do I need to import it? – J-Cake Apr 19 '18 at 07:01
  • Argparse can not only parse flags but also positional arguments as you would like. There are many argument parsing libraries (argparse, docopt, click, invoke, optik,...) and all should be able to do what you want. – syntonym Apr 19 '18 at 07:01
  • Possible duplicate of [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – Peter Wood Apr 19 '18 at 07:06

1 Answers1

0

For this kind of task I would use Click because of its simplicity and readability.

Here's an example that fits your needs for the argument "name":

@click.command()
@click.option('--count', default=1, help='number of greetings')
@click.argument('name')
def hello(count, name):
    for x in range(count):
        click.echo('Hello %s!' % name)

Please note the difference here between option and argument.

A simpler example, using standard libraries, would be:

import sys

print "1st argument is " + sys.argv[1]
Pitto
  • 8,229
  • 3
  • 42
  • 51
  • My pleasure, @JacobSchneider! Please remember to upvote and/or choose the answer if you found it useful. – Pitto Apr 19 '18 at 07:24
  • I haven't forgotten, but every time I try it tells me I can accept the answer in 10 minutes. – J-Cake Apr 19 '18 at 07:25
  • 1
    I haven't forgotten, but every time I try it tells me I can accept the answer in 10 minutes. But it works now – J-Cake Apr 19 '18 at 07:27