argparse-from-file


Nameargparse-from-file JSON
Version 1.6 PyPI version JSON
download
home_pageNone
SummaryWrapper for Python's argparse to also read arguments from a file
upload_time2025-10-13 00:46:58
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords argparse configargparse
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ARGPARSE-FROM-FILE
[![PyPi](https://img.shields.io/pypi/v/argparse-from-file)](https://pypi.org/project/argparse-from-file/)
[![AUR](https://img.shields.io/aur/version/python-argparse-from-file)](https://aur.archlinux.org/packages/python-argparse-from-file/)

`argparse-from-file` is a lightweight wrapper for Python's standard
[`argparse`][argparse] module. It allows your program to read default arguments
from a configuration file, which are prepended to arguments provided on the
command line.

The latest version of this document and code is available at
https://github.com/bulletmark/argparse-from-file.

## Features

* **Drop-in replacement:** Simply change `import argparse` to `import
  argparse_from_file as argparse`. No other code changes are needed for basic
  functionality.
* **Automatic configuration:** By default, it loads arguments from
  `<program_name>-flags.conf` in the user's configuration directory (e.g.,
  `~/.config/` on Linux). The exact path is determined using the
  [`platformdirs`][platformdirs] library to respect OS conventions.
* **Custom configuration file:** Specify a custom file path with the
  `from_file` argument to [`ArgumentParser()`][argparser].
* **Simple file format:** The configuration file is a simple text file with
  options specified on one or more lines. Blank lines and lines starting with
  `#` are ignored.
* **Informative help text:** The program's help message is automatically
  updated to show the path to the configuration file.

## Usage

To get started, replace the standard [`argparse`][argparse] import in your project:

```python
# import argparse
import argparse_from_file as argparse
```

That's it! Your application will now automatically look for a default
configuration file and use the default arguments provided therein.

To specify a custom configuration file, use the `from_file` keyword argument,
which is the only addition to the standard
[`ArgumentParser()`][argparser] API:

```python
# Use a specific () file path
parser = argparse.ArgumentParser(from_file='/path/to/myprog.conf', ..)

# Use a file relative to the user's config directory
parser = argparse.ArgumentParser(from_file='myprog.conf', ..)
```

The `from_file` argument accepts a string or a [`pathlib.Path`][pathlib].
Relative paths are resolved relative to the user's configuration directory as
determined by `platformdirs`.

## Configuration File Format

Arguments in the configuration file are best specified one per line so they can
easily be commented out. It is also recommended to use long-form options for
clarity.

Example `~/.config/myprog-flags.conf`:
```
# Always run with foo set to 123
--foo 123

# Default to verbose output
--verbose
```

## Customizing the Help Message

`argparse-from-file` automatically adds an `epilog` to the help message
indicating the configuration file path. If you instead provide a custom `epilog`
(or `usage` or `description`), you can embed a `#FROM_FILE_PATH#` placeholder,
and it will be replaced with the actual path used.

## Example

Here is a simple example program (`myprog.py`):

```python
#!/usr/bin/python3
import argparse_from_file as argparse

parser = argparse.ArgumentParser()
parser.add_argument('--foo', type=int, default=42, help='foo help')
opts = parser.parse_args()

print(f"foo is: {opts.foo}")
```

If `~/.config/myprog-flags.conf` contains `--foo=123`, the output is:

```
$ python myprog.py
foo is: 123
```

The help text (`epilog`) is also automatically added as seen below:
```
$ python myprog.py -h
usage: myprog.py [-h] [--foo FOO]

options:
  -h, --help  show this help message and exit
  --foo FOO   foo help

Note you can set default starting options in /home/user/.config/myprog-flags.conf.
```

## Installation

Arch Linux users can install `python-argparse-from-file` from the
[AUR](https://aur.archlinux.org/packages/python-argparse-from-file/).

Alternatively, `argparse-from-file` is available on
[PyPI](https://pypi.org/project/argparse-from-file/) and can be installed with
pip:

```bash
pip install argparse-from-file
```

## License

Copyright (C) 2025 Mark Blakeney. This program is distributed under the terms
of the GNU General Public License. This program is free software: you can
redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the
License, or any later version. This program is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License at <https://opensource.org/license/gpl-3-0> for more details.

[argparse]: https://docs.python.org/3/library/argparse.html
[platformdirs]: https://github.com/tox-dev/platformdirs
[argparser]: https://docs.python.org/3/library/argparse.html#argumentparser-objects
[pathlib]: https://docs.python.org/3/library/pathlib.html
<!-- vim: se ai syn=markdown: -->

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "argparse-from-file",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "argparse, ConfigArgParse",
    "author": null,
    "author_email": "Mark Blakeney <mark.blakeney@bullet-systems.net>",
    "download_url": "https://files.pythonhosted.org/packages/18/5c/45ad5138bc8b1c844cac5d5943765d358d4f287ffee679becca634ea0373/argparse_from_file-1.6.tar.gz",
    "platform": null,
    "description": "# ARGPARSE-FROM-FILE\n[![PyPi](https://img.shields.io/pypi/v/argparse-from-file)](https://pypi.org/project/argparse-from-file/)\n[![AUR](https://img.shields.io/aur/version/python-argparse-from-file)](https://aur.archlinux.org/packages/python-argparse-from-file/)\n\n`argparse-from-file` is a lightweight wrapper for Python's standard\n[`argparse`][argparse] module. It allows your program to read default arguments\nfrom a configuration file, which are prepended to arguments provided on the\ncommand line.\n\nThe latest version of this document and code is available at\nhttps://github.com/bulletmark/argparse-from-file.\n\n## Features\n\n* **Drop-in replacement:** Simply change `import argparse` to `import\n  argparse_from_file as argparse`. No other code changes are needed for basic\n  functionality.\n* **Automatic configuration:** By default, it loads arguments from\n  `<program_name>-flags.conf` in the user's configuration directory (e.g.,\n  `~/.config/` on Linux). The exact path is determined using the\n  [`platformdirs`][platformdirs] library to respect OS conventions.\n* **Custom configuration file:** Specify a custom file path with the\n  `from_file` argument to [`ArgumentParser()`][argparser].\n* **Simple file format:** The configuration file is a simple text file with\n  options specified on one or more lines. Blank lines and lines starting with\n  `#` are ignored.\n* **Informative help text:** The program's help message is automatically\n  updated to show the path to the configuration file.\n\n## Usage\n\nTo get started, replace the standard [`argparse`][argparse] import in your project:\n\n```python\n# import argparse\nimport argparse_from_file as argparse\n```\n\nThat's it! Your application will now automatically look for a default\nconfiguration file and use the default arguments provided therein.\n\nTo specify a custom configuration file, use the `from_file` keyword argument,\nwhich is the only addition to the standard\n[`ArgumentParser()`][argparser] API:\n\n```python\n# Use a specific () file path\nparser = argparse.ArgumentParser(from_file='/path/to/myprog.conf', ..)\n\n# Use a file relative to the user's config directory\nparser = argparse.ArgumentParser(from_file='myprog.conf', ..)\n```\n\nThe `from_file` argument accepts a string or a [`pathlib.Path`][pathlib].\nRelative paths are resolved relative to the user's configuration directory as\ndetermined by `platformdirs`.\n\n## Configuration File Format\n\nArguments in the configuration file are best specified one per line so they can\neasily be commented out. It is also recommended to use long-form options for\nclarity.\n\nExample `~/.config/myprog-flags.conf`:\n```\n# Always run with foo set to 123\n--foo 123\n\n# Default to verbose output\n--verbose\n```\n\n## Customizing the Help Message\n\n`argparse-from-file` automatically adds an `epilog` to the help message\nindicating the configuration file path. If you instead provide a custom `epilog`\n(or `usage` or `description`), you can embed a `#FROM_FILE_PATH#` placeholder,\nand it will be replaced with the actual path used.\n\n## Example\n\nHere is a simple example program (`myprog.py`):\n\n```python\n#!/usr/bin/python3\nimport argparse_from_file as argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--foo', type=int, default=42, help='foo help')\nopts = parser.parse_args()\n\nprint(f\"foo is: {opts.foo}\")\n```\n\nIf `~/.config/myprog-flags.conf` contains `--foo=123`, the output is:\n\n```\n$ python myprog.py\nfoo is: 123\n```\n\nThe help text (`epilog`) is also automatically added as seen below:\n```\n$ python myprog.py -h\nusage: myprog.py [-h] [--foo FOO]\n\noptions:\n  -h, --help  show this help message and exit\n  --foo FOO   foo help\n\nNote you can set default starting options in /home/user/.config/myprog-flags.conf.\n```\n\n## Installation\n\nArch Linux users can install `python-argparse-from-file` from the\n[AUR](https://aur.archlinux.org/packages/python-argparse-from-file/).\n\nAlternatively, `argparse-from-file` is available on\n[PyPI](https://pypi.org/project/argparse-from-file/) and can be installed with\npip:\n\n```bash\npip install argparse-from-file\n```\n\n## License\n\nCopyright (C) 2025 Mark Blakeney. This program is distributed under the terms\nof the GNU General Public License. This program is free software: you can\nredistribute it and/or modify it under the terms of the GNU General Public\nLicense as published by the Free Software Foundation, either version 3 of the\nLicense, or any later version. This program is distributed in the hope that it\nwill be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public\nLicense at <https://opensource.org/license/gpl-3-0> for more details.\n\n[argparse]: https://docs.python.org/3/library/argparse.html\n[platformdirs]: https://github.com/tox-dev/platformdirs\n[argparser]: https://docs.python.org/3/library/argparse.html#argumentparser-objects\n[pathlib]: https://docs.python.org/3/library/pathlib.html\n<!-- vim: se ai syn=markdown: -->\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Wrapper for Python's argparse to also read arguments from a file",
    "version": "1.6",
    "project_urls": {
        "Homepage": "https://github.com/bulletmark/argparse-from-file"
    },
    "split_keywords": [
        "argparse",
        " configargparse"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0631d95a2e8bf0363f154206ed859c494dbfc7d3807aa198b3555af1ca6032ef",
                "md5": "34bb2fd532c9194b2346c8a07588f0ee",
                "sha256": "43d1845d97ba5808600a3a70cc175fc7c2a9eb9ea2441c95eb27e59920a97c21"
            },
            "downloads": -1,
            "filename": "argparse_from_file-1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "34bb2fd532c9194b2346c8a07588f0ee",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5162,
            "upload_time": "2025-10-13T00:46:57",
            "upload_time_iso_8601": "2025-10-13T00:46:57.061824Z",
            "url": "https://files.pythonhosted.org/packages/06/31/d95a2e8bf0363f154206ed859c494dbfc7d3807aa198b3555af1ca6032ef/argparse_from_file-1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "185c45ad5138bc8b1c844cac5d5943765d358d4f287ffee679becca634ea0373",
                "md5": "7b331ec9b10dd3813a9f49f3e8246fd4",
                "sha256": "378ec171a783a6f52123e90b5dd89992ec5ce4df43809f88216da02c973d9db8"
            },
            "downloads": -1,
            "filename": "argparse_from_file-1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "7b331ec9b10dd3813a9f49f3e8246fd4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 5443,
            "upload_time": "2025-10-13T00:46:58",
            "upload_time_iso_8601": "2025-10-13T00:46:58.504198Z",
            "url": "https://files.pythonhosted.org/packages/18/5c/45ad5138bc8b1c844cac5d5943765d358d4f287ffee679becca634ea0373/argparse_from_file-1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-13 00:46:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bulletmark",
    "github_project": "argparse-from-file",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "argparse-from-file"
}
        
Elapsed time: 1.49351s