3

as far as I understood, with ConfigArgParse, I can set the very main config in a config.ini file of my program and make some of those choices available via command line. However, when I set my config.ini file as default in the constructor, I get the following error:

main.py: error: unrecognized arguments: --input_base data

where --input_base is the only configuration not included in my parser as can be seen in the following:

parser = ArgParser(default_config_files=['config.ini'])
parser.add_argument('-out', '--output_base', type=str, help='xyz')
parser.add_argument('--amount', type=int, help='xyz')
parser.add_argument('--num_jobs', help='xyz')
parser.add_argument('--batch_size', type=int, help='xyz')
parser.add_argument('--queue_size', type=int, help='xyz')
parser.add_argument('--kind', choices={'long', 'short', 'both'}, help='xyz')
parser.add_argument('--level', choices={'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}, help='xyz')
config = parser.parse_args()

Only using config.ini works fine but because of usability I have to include command line args as well.

Thanks for your help. Appreciate it!

turnman
  • 154
  • 10
  • What's your command line? I don't know what `ConfigArgParse` adds to `argparse`, but evidently the 'unrecognized arguments' are in the `sys.argv` list that your parser is trying to parse. Make sure you understand why. – hpaulj Sep 05 '18 at 16:05
  • Could you maybe give me a little more info what you mean? the unknown arguments come from the `config.ini` file mentioned in the first line with `parser = ...`. However, I did not know how to access those correctly. – turnman Nov 09 '18 at 12:46

1 Answers1

4

Try change last line to:

config, unknown = parser.parse_known_args()

This will parse only known arguments (ignoring every unknown).

as in this question: Python argparse ignore unrecognised arguments

yaroslaff
  • 81
  • 5
  • Great, thank you! I think this is what I was looking for. Is there anyway to access those arguments in an elegant way like I can do it with those in `config` with `config.arg`? PS: sorry for the late reply, I was caught up with something else – turnman Nov 09 '18 at 12:43
  • unknown is just simple list of strings. You can use it this way, for example: `if '--unknown' in unknown:`. But there is no other way, because processing possible only for known args (which provides rules, how to process arguments) – yaroslaff Nov 14 '18 at 17:14