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

Feature: Make argument flag configurable #414

Closed
guarin opened this issue Sep 20, 2024 · 5 comments · Fixed by #418
Closed

Feature: Make argument flag configurable #414

guarin opened this issue Sep 20, 2024 · 5 comments · Fixed by #418
Assignees

Comments

@guarin
Copy link

guarin commented Sep 20, 2024

Hi, would it be possible to make the argument flag (the -- prefix before argument names) configurable? I saw that it is hardcoded here:

arg_flag: str = '--'

Our use case is that we are migrating some tools from omegaconf to pydantic and would like to use pydantic-settings to parse CLI arguments. My understanding is that pydantic-settings always adds a -- prefix before argument names whereas omegaconf doesn't add this prefix. It would be great to have an option to disable the prefix in pydantic-settings to make the transition backwards compatible.

I also saw that -- is used in another place but am not sure if this would be relevant for this feature:

matched = re.match(r'^(--[^\s=]+)(.*)', arg)

@hramezani
Copy link
Member

Thanks @guarin for requesting this feature.

I think -- is very common as a prefix. I am not sure that making this configurable would be a good idea.

@kschwab what do you think?

@kschwab
Copy link
Contributor

kschwab commented Sep 21, 2024

@hramezani I agree that -- is most common. I think this is mostly a maintainer question on what CLI features to support. Since we are argparse based, it is not much trouble to add since argparse supports this with prefix chars. I'll put up a PR for reference.

@guarin
Copy link
Author

guarin commented Sep 21, 2024

Thanks so much for the PR! I just tried it out and it seems that argparse doesn't support having no prefix chars prefix_chars="", so parsing arguments like arg=5 doesn't work (this is sadly how omegaconf expects arguments). I guess this is out of scope as it would require changes in argparse itself.

@mpkocher
Copy link

It would be great to have an option to disable the prefix in pydantic-settings to make the transition backwards compatible.

What does "disable" the prefix mean? I believe the prefix has to be non-empty. Argparse will probably do some wonky things if you step off the garden path.

In [1]: import argparse

In [2]: for x in ('+', 'a'):
   ...:     p = argparse.ArgumentParser(prefix_chars=x)
   ...:     p.add_argument('i')
   ...:     p.add_argument('aj')
   ...:     p.print_help()
   ...: 
usage: ipython [+h] i aj

positional arguments:
  i
  aj

options:
  +h, ++help  show this help message and exit
usage: ipython [ah] [aj J] i

positional arguments:
  i

options:
  ah, aahelp  show this help message and exit
  aj J

Empty char:

In [4]: argparse.ArgumentParser(prefix_chars="")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 argparse.ArgumentParser(prefix_chars="")
-> 1805 default_prefix = '-' if '-' in prefix_chars else prefix_chars[0]
   1806 if self.add_help:
   1807     self.add_argument(
   1808         default_prefix+'h', default_prefix*2+'help',
   1809         action='help', default=SUPPRESS,
   1810         help=_('show this help message and exit'))

IndexError: string index out of range

@kschwab
Copy link
Contributor

kschwab commented Sep 21, 2024

@guarin, yes, it's a limitation of argparse, as well as most CLI parsing libraries. It would be a larger effort to support in CliSettingsSource.

If your use case allows for it, you could always use environment variables to mimic a CLI. e.g., on Linux the below invocations are equivalent when using Pydantic Settings:

$ python3 my_app.py --arg=5 --other_arg=6
$ arg=5 other_arg=6 python3 my_app.py

On Windows, using env vars to mimic the CLI is uglier, and could really only be done in a similar manner using PowerShell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants