Wrapper for Python's argparse to also read arguments from a file
Project description
ARGPARSE-FROM-FILE
argparse-from-file is a lightweight wrapper for Python's standard
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 argparsetoimport 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.confin the user's configuration directory (e.g.,~/.config/on Linux). The exact path is determined using theplatformdirslibrary to respect OS conventions. - Custom configuration file: Specify a custom file path with the
from_fileargument toArgumentParser(). - 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 import in your project:
# 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() API:
# Use a specific file path
parser = argparse.ArgumentParser(from_file='/path/to/my/config', ..)
# Use a file relative to the user's config directory
parser = argparse.ArgumentParser(from_file='my-app.conf', ..)
The from_file argument accepts a string or a pathlib.Path.
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:
--verbose
# Always run with foo set to 123
--foo 123
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 the #FROM_FILE_PATH# placeholder,
and it will be replaced with the actual path used.
Example
Here is a simple example program (myprog.py):
#!/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.
Alternatively, argparse-from-file is available on
PyPI and can be installed with
pip:
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparse_from_file-1.0.tar.gz.
File metadata
- Download URL: argparse_from_file-1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
716f5c11f0f1486d5fa8cae154584a77f89d09836d525766e4202963223d8e48
|
|
| MD5 |
cea18cec20aedb85d781d4a08d697bbe
|
|
| BLAKE2b-256 |
9fd1df1124ab9399c085ff200cc1de9b9e0aae9d106950baeeca13ebf884f97e
|
File details
Details for the file argparse_from_file-1.0-py3-none-any.whl.
File metadata
- Download URL: argparse_from_file-1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77992982b3f2a96dbedb504135153de8cb5f5b7a1ce28bf66257ba0585483337
|
|
| MD5 |
b861b92b0e44056c39ab627827de139b
|
|
| BLAKE2b-256 |
867600bed6790d81bc67423fef19e286d786a6f54eb2291b5c642d13269f4f61
|