Generate PowerShell wrapper scripts from Python argparse parsers
argparse-ps1 automatically generates PowerShell (.ps1) wrapper scripts for Python scripts that use argparse. This provides native PowerShell tab completion and parameter binding for your Python scripts.
- 🚀 Automatic Generation: Creates PowerShell wrappers from
ArgumentParserobjects - 🎯 Type Mapping: Maps Python types to PowerShell parameter types
- 📝 Native Interface: Provides PowerShell-style parameter names and help
- ⚙️ Multiple Runners: Supports
uv,python, or custom runners - 🔧 Zero Dependencies: Uses only Python standard library
pip install argparse-ps1Add wrapper generation to your Python script:
from argparse_ps1 import generate_ps1_wrapper
# Your existing argparse code
parser = argparse.ArgumentParser(description="My script")
parser.add_argument("--hello", action="store_true", help="Say hello")
parser.add_argument("--option", type=str, help="String option")
# Generate PowerShell wrapper
if "--make-ps1" in sys.argv:
output = generate_ps1_wrapper(parser, script_path=Path(__file__))
print(f"Generated: {output}")
sys.exit(0)Generate the wrapper:
python my_script.py --make-ps1
# Creates My-Script.ps1Use the PowerShell wrapper:
.\My-Script.ps1 -Hello -Option "test"See the examples/ directory for complete working examples:
- basic_example.py: Simple boolean flags
- example.py: String options with
uv run - example_uv_project.py: Using
uv run --project - example_python.py: Using
pythonrunner instead ofuv
For project mode examples (example_uv_project.py), you need:
- Proper pyproject.toml structure with
[project.scripts]entry - Package structure with
__init__.pyfiles - Correct execution method: Use
uv run python example_uv_project.pyinstead of script entries
generate_ps1_wrapper(parser, script_path=Path(__file__))
# Creates: & uv run script.py @argsgenerate_ps1_wrapper(
parser,
script_path=Path(__file__),
command_name="my-command" # Must exist in [project.scripts]
)
# Creates: & uv run --project <project-root> my-command @argsUse alternative Python executables instead of uv:
# System Python
generate_ps1_wrapper(
parser,
script_path=Path(__file__),
runner="python"
)
# Creates: & python script.py @args
# Virtual environment Python (relative path)
generate_ps1_wrapper(
parser,
script_path=Path(__file__),
runner=".venv/Scripts/python.exe" # Windows
# runner=".venv/bin/python" # Unix/macOS
)
# Creates: & .venv\Scripts\python.exe script.py @args
# Custom Python installation (absolute path)
generate_ps1_wrapper(
parser,
script_path=Path(__file__),
runner="C:/Python312/python.exe"
)
# Creates: & "C:/Python312/python.exe" script.py @argsNote: See example_python.py for a complete working example.
def generate_ps1_wrapper(
parser: argparse.ArgumentParser,
*,
script_path: Path,
output_path: Path | None = None,
output_dir: Path | None = None,
skip_dests: Iterable[str] | None = None,
runner: str = "uv",
command_name: str | None = None,
) -> PathParameters:
parser: YourArgumentParserinstancescript_path: Path to your Python script (absolute path required)output_path: Specific output file path (optional, overrides output_dir)output_dir: Output directory (default: current working directory)skip_dests: Argument destinations to exclude from wrapperrunner: Command to run Python scripts (default: "uv", can be "python")command_name: Command name in[project.scripts](enables project mode for uv)
| Python Type | PowerShell Type | Example |
|---|---|---|
str |
[string] |
-Name "value" |
int |
[int] |
-Count 42 |
float |
[double] |
-Rate 3.14 |
Path |
[string] |
-File "path/to/file" |
store_true |
[switch] |
-Verbose |
- Python 3.11+
- Windows PowerShell or PowerShell Core
MIT License - see LICENSE file for details.
Contributions welcome! Please see DEVELOPMENT.md for setup instructions.