Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Root Args and config file #232

Closed
efanibi25 opened this issue Jan 18, 2023 · 5 comments · Fixed by #233
Closed

Issue with Root Args and config file #232

efanibi25 opened this issue Jan 18, 2023 · 5 comments · Fixed by #233
Labels
bug Something isn't working

Comments

@efanibi25
Copy link

efanibi25 commented Jan 18, 2023

🐛 Bug report

An error
jsonargparse.util.ParserError: Problem in default config file "/home/main/Tools/.config/config.yaml" :: Configuration check failed :: No action for destination key "prepare.output" to check its value.
is raised when trying to parse args

To reproduce

parser = ArgumentParser(default_config_files=[os.path.join('.config/config.yaml')])
    parser.add_argument('-c','--config', action=ActionConfigFile,help="override config file")  
    parser.add_argument("-o","--output",help="Path to Dir",required=True)
    prepare = ArgumentParser(description="Prepare file to be uploaded")
    prepare.add_argument('-m','--media',help="Directory to retrive media files",required=True)
  subcommands = parser.add_subcommands()
    subcommands.add_subcommand('prepare',prepare, help="prepare mediafile for upload")

root argument must have required=True
root argument must also come before any group arguments.

Also Note: I have been able to fix this by setting root args to be the last argument

It strange because it also can parses correctly if I set required=False

Expected behavior

Parse arguments

Environment

  • jsonargparse version 4.19.0
  • Python version 3.8
  • How jsonargparse was installed (e.g. `pip install jsonargparse):
  • OS Linux
@efanibi25 efanibi25 added the bug Something isn't working label Jan 18, 2023
@mauvilsa
Copy link
Member

Thank you very much for reporting. I don't quite understand the description and tried to reproduce the problem but unsuccessfully. Can you please improve the reproduction section, such that:

  • the code snippet is correct: all imports, valid indentation, inclusion of parser.parse_args
  • how to run the code to get the error, i.e. $ python cli.py ...

What I tried to do was to run the code

import os
from jsonargparse import ArgumentParser, ActionConfigFile

parser = ArgumentParser(default_config_files=[os.path.join('.config/config.yaml')])
parser.add_argument('-c','--config', action=ActionConfigFile,help="override config file")  
parser.add_argument("-o","--output",help="Path to Dir",required=True)
prepare = ArgumentParser(description="Prepare file to be uploaded")
prepare.add_argument('-m','--media',help="Directory to retrive media files",required=True)
subcommands = parser.add_subcommands()
subcommands.add_subcommand('prepare',prepare, help="prepare mediafile for upload")

print(parser.parse_args())

using as command

$ python cli.py -o . prepare --media value
Namespace(config=None, output='.', prepare=Namespace(media='value'), subcommand='prepare')

It executes without issue printing the namespace. Note that I ran it in a directory that only has the cli.py file and no configuration files. If the bug depends on there being config files, please also describe this.

@efanibi25
Copy link
Author

efanibi25 commented Jan 18, 2023

Okay yeah here is a better description
Yes the bug does depend on the inclusion of a config file

Code
filename:test.py

content

import os
from jsonargparse import ArgumentParser, ActionConfigFile

parser = ArgumentParser(default_config_files=['.config/config.yaml'])
parser.add_argument('-c','--config', action=ActionConfigFile,help="override config file")  
parser.add_argument("-o","--output",help="Path to Dir",required=True)
prepare = ArgumentParser(description="Prepare file to be uploaded")
prepare.add_argument('-m','--media',help="Directory to retrive media files",required=True)
subcommands = parser.add_subcommands()
subcommands.add_subcommand('prepare',prepare, help="prepare mediafile for upload")

print(parser.parse_args())

configfile
path:config/config.yaml

content

output: test
prepare: 
    media: test

How to Trigger/Run
test.py prepare

ErrorRaised

Exception has occurred: ParserError
Problem in default config file ".config/config.yaml" :: Configuration check failed :: No action for destination key "prepare.media" to check its value.

The above exception was the direct cause of the following exception:


The above exception was the direct cause of the following exception:

  File "/src/argstest/test.py", line 9, in <module>
    subcommands = parser.add_subcommands()

@efanibi25
Copy link
Author

efanibi25 commented Jan 18, 2023

As a note changing the code to this does work

========================================
import os
from jsonargparse import ArgumentParser, ActionConfigFile

parser = ArgumentParser(default_config_files=['.config/config.yaml'])
parser.add_argument('-c','--config', action=ActionConfigFile,help="override config file")
prepare = ArgumentParser(description="Prepare file to be uploaded")
prepare.add_argument('-m','--media',help="Directory to retrive media files",required=True)
subcommands = parser.add_subcommands()
subcommands.add_subcommand('prepare',prepare, help="prepare mediafile for upload")
parser.add_argument("-o","--output",help="Path to Dir",required=True)

print(parser.parse_args())

==================================

I just moved the root arguments to be added last

@mauvilsa
Copy link
Member

I created a pull request fixing the issue, see #233. Please review the changes and test the code. You can install directly from that branch by running:

pip install "jsonargparse @ https://github.com/omni-us/jsonargparse/zipball/issue-232"

@mauvilsa
Copy link
Member

Note that the problem is parser.add_subcommands() so another workaround is to first call add_subcommands and then add arguments.

mauvilsa added a commit that referenced this issue Jan 20, 2023
mauvilsa added a commit that referenced this issue Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants