0

When I run the following code:

def read_args():
    parser = default_parser()
    parser.add_argument('--tensorboard-dir', type=str, default='/tmp/cifar10/tensorboard')
    parser.add_argument('-N', type=int, default=50000, help="Use N training examples.")

    return parser.parse_args()

def main():
    flags = readargs()

I have the following error output:

The following arguments are required: --name

Error Output

However when I add the --name argument:

def read_args():
    parser = default_parser()
    parser.add_argument('--name', type=str, default='cifar10test')
    parser.add_argument('--tensorboard-dir', type=str, default='/tmp/cifar10/tensorboard')
    parser.add_argument('-N', type=int, default=50000, help="Use N training examples.")

    return parser.parse_args()


def main():
    flags = readargs()

is also creating problem.

Any ideas?

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • what is `default_parser`? How exactly are you running this? and lastly, the error doesn't say you need to **define** the argument `name`, rather to **add** it in your command line call. If you mean you *literaly* run the above code, then no arguments are passed. change to `parser.parse_args([...])` and fill the list with the arguments you want to pass (all strings) – Tomerikoo Jul 16 '19 at 14:37
  • The error shows a 'usage' as well. What is it? – hpaulj Jul 16 '19 at 14:39
  • error means that you have to run script as `script.py --name some_value` . Probably in `default_parser()` you have `add_argument('--name', ...)`. Run `script.py --help` to see all arguments. – furas Jul 16 '19 at 15:01
  • 2
    When you tried to add your own `--name` argument, what was the error? I suspect it complained about a conflicting definition. That would confirm the idea that `default_parser` has already defined a `--name` (as required). – hpaulj Jul 16 '19 at 17:07
  • @Tomerikoo Dear Tomerikoo, default parser method contains a massive amount of argument variables, and I did not add it, because I thought it might be confusing. Would you like me to add the default_parser method? – Ahmet Jul 17 '19 at 08:47
  • @AhmetTavli No, no need to put it all here. Simply check if there is already a `--name` argument there, which is most likely the case... – Tomerikoo Jul 17 '19 at 09:28

1 Answers1

4

It appears that default_parser contains a --name argument which is required. What you're doing in your second example is defining the argument twice - once in default_parser and once in your program. Instead, you should be passing a --name argument when calling your program from the command line.

Example:

python cifar.py -N=1200 --tensorboard-dir=file.txt --name=cool_name

Alternatively, you could remove default_parser and construct your own ArgumentParser:

`parser = argparse.ArgumentParser()`

Full working demo:

import argparse

def read_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--tensorboard-dir', type=str,
                        default='/tmp/cifar10/tensorboard')
     parser.add_argument('-N', type=int, default=50000,
                         help="Use N training examples.")

     return parser.parse_args()

def main():
    flags = vars(read_args())
    # You can access your args as a dictionary
    for key in flags:
        print("{} is {}".format(key, flags[key]))

main()

The parser returns a Namespace object, but we can access its (much simpler) internal dictionary using vars(Namespace). You can then get your arguments by accessing the dictionary, for example, flags['N']. Note that tensorboard-dir becomes tensorboard_dir inside your python program to avoid issues with the subtraction operator.

Call it from the command line (I'm using Bash):

python cifar.py -N=1200 --tensorboard-dir=file.txt

Output:

tensorboard_dir is file.txt
N is 1200
Tom Dufall
  • 871
  • 1
  • 10
  • 21
  • As someone pointed out in the comments on the question, I guess you're importing `default_parser()` from elsewhere. If you include the source then someone can help out, but I'm guessing it has a required `--name` argument. If so, you need to be adding that argument when calling the program from the command line, not trying to override it by adding it as another argument. – Tom Dufall Jul 16 '19 at 15:39
  • 1
    He doesn't need to do the `sys.argv[1:]` stuff, because that's the default. `print(flags)` is enough to see the Namespace. But none of that addresses the '--name' error. – hpaulj Jul 16 '19 at 17:10
  • Given he had no idea what `--name` was and wasn't referencing it anywhere else, I assumed he didn't want it and was just reusing a piece of code. You're right about `sys.argv` (I'm not very familiar with argparse) - I'm updating the answer entirely to reflect that. – Tom Dufall Jul 17 '19 at 10:24