Repository: omni-us/jsonargparse

Issue:
Issue with Root Args and config file
## 🐛 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


Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 0aef214..58bf380 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -15,6 +15,11 @@ paths are considered internals and can change in minor and patch releases.
 v4.20.0 (2023-01-??)
 --------------------
 
+Fixed
+^^^^^
+- ``add_subcommands`` fails when parser has required argument and default config
+  available `#232 <https://github.com/omni-us/jsonargparse/issues/232>`__.
+
 Changed
 ^^^^^^^
 - When parsing fails now ``argparse.ArgumentError`` is raised instead of
diff --git a/jsonargparse/core.py b/jsonargparse/core.py
index a951acb..3420ba4 100644
--- a/jsonargparse/core.py
+++ b/jsonargparse/core.py
@@ -670,7 +670,7 @@ class ArgumentParser(ParserDeprecations, ActionsContainer, ArgumentLinking, argp
         """
         if 'description' not in kwargs:
             kwargs['description'] = 'For more details of each subcommand, add it as an argument followed by --help.'
-        with parser_context(parent_parser=self):
+        with parser_context(parent_parser=self, lenient_check=True):
             subcommands: _ActionSubCommands = super().add_subparsers(dest=dest, **kwargs)  # type: ignore
         if required:
             self.required_args.add(dest)


Test patch:
diff --git a/jsonargparse_tests/test_core.py b/jsonargparse_tests/test_core.py
index eb154e0..b76dfe1 100755
--- a/jsonargparse_tests/test_core.py
+++ b/jsonargparse_tests/test_core.py
@@ -2,6 +2,7 @@
 
 import json
 import os
+import pathlib
 import pickle
 import sys
 import unittest
@@ -1012,6 +1013,19 @@ class ConfigFilesTests(TempDirTestCase):
         self.assertIn('defaults_2.yaml', out.getvalue())
 
 
+    def test_required_arg_in_default_config_and_add_subcommands(self):
+        pathlib.Path('config.yaml').write_text('output: test\nprepare:\n  media: test\n')
+        parser = ArgumentParser(default_config_files=['config.yaml'])
+        parser.add_argument('--output', required=True)
+        subcommands = parser.add_subcommands()
+        prepare = ArgumentParser()
+        prepare.add_argument('--media', required=True)
+        subcommands.add_subcommand('prepare', prepare)
+        cfg = parser.parse_args([])
+        self.assertEqual(str(cfg.__default_config__), 'config.yaml')
+        self.assertEqual(strip_meta(cfg), Namespace(output='test', prepare=Namespace(media='test'), subcommand='prepare'))
+
+
     def test_ActionConfigFile(self):
         os.mkdir(os.path.join(self.tmpdir, 'subdir'))
         rel_yaml_file = os.path.join('subdir', 'config.yaml')