Skip to main content

ARGPARSE-FROM-FILE

PyPi AUR

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 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 library to respect OS conventions.
  • Custom configuration file: Specify a custom file path with the from_file argument to ArgumentParser().
  • 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/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. 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):

#!/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 ~/.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.

Metadata

Release files for argparse-from-file 1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argparse-from-file 1.8
File Size Uploaded
argparse_from_file-1.8.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for argparse-from-file 1.8
File Interpreter ABI Platform
argparse_from_file-1.8-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / argparse_from_file-1.8.tar.gz

Download URL argparse_from_file-1.8.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2c2e98e22cd2757f1b998559691c78cc4827e578774b6cad92e5e7bb3a0d02ce
BLAKE2b-256 checksum
How to use checksums
2fb00d627b8fcd85b18d1dc0b2f4f7ceb75ccb12d7b45d6806bddbdbbb88f218
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / argparse_from_file-1.8-py3-none-any.whl

Download URL argparse_from_file-1.8-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2121e0cbc00c846aacc92378afc62d427919f015eff71056df720d723643f304
BLAKE2b-256 checksum
How to use checksums
07ec6cc47161f6be401b7e6b21488a10419d77068ac5e49f904ae51f7e97e122
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.8 This release

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page