Parser Commands

Pattern: use --input/--output first, else open pickers; allow .env defaults

Info

  • Common flags: --input (files) and --output (folder).
  • Flow: read args → if missing, open file/folder dialogs.
  • Defaults may come from env (.env or system variables).
  • .bat can pass args to avoid retyping them each time.

Example usage

Direct paths and using a Windows env variable for output:

Bash example

python example.py --input "folder/path/example.mp4" --output "folder/export"

Batch example

python example.py --input "folder/path/example.mp4" --output %OUTPUT_DIR%

Minimal template

import os, argparse, sys
from pathlib import Path

# Optional: load .env from script dir, then parent, then grandparent
def load_env():
    try:
        from dotenv import load_dotenv  # pip install python-dotenv
    except ImportError:
        return
    base = Path(__file__).resolve().parent
    for p in [base / ".env", base.parent / ".env", base.parent.parent / ".env"]:
        if p.exists():
            load_dotenv(p)
            break

def build_parser() -> argparse.ArgumentParser:
    p = argparse.ArgumentParser(description="Process files")
    p.add_argument("--input", "-i", nargs="+", help="Input files")
    p.add_argument("--output", "-o", help="Output folder",
                   default=os.getenv("OUTPUT_DIR", ""))  # env default if present
    return p

def pick_inputs_and_output(args):
    # Only prompt if not provided on CLI
    inputs, output = args.input, args.output
    if not inputs:
        try:
            import tkinter as tk
            from tkinter import filedialog
            tk.Tk().withdraw()
            inputs = filedialog.askopenfilenames(title="Select input files")
        except Exception:
            print("Provide --input files; GUI not available.")
            sys.exit(2)
    if not output:
        # default: same folder as first input
        output = str(Path(inputs[0]).resolve().parent)
        try:
            import tkinter as tk
            from tkinter import filedialog
            tk.Tk().withdraw()
            chosen = filedialog.askdirectory(title="Select output folder (optional)")
            if chosen:
                output = chosen
        except Exception:
            pass
    return list(inputs), output

def main():
    load_env()
    args = build_parser().parse_args()
    inputs, output = pick_inputs_and_output(args)
    print("Inputs:", inputs)
    print("Output:", output)
    # ... your processing ...

if __name__ == "__main__":
    main()

Passing args from .bat (optional)

Set --input/--output in the .bat when known:

Batch example

@echo off
setlocal
REM Example: put this next to my_script.py and name it my_script.bat
set "HERE=%~dp0"
REM Known paths (or read from env)
REM set "OUTPUT_DIR=F:\Out"

REM Pass args so the script skips dialogs
python "%HERE%my_script.py" --input "%HERE%in\a.png" "%HERE%in\b.png" --output "%HERE%out"
if errorlevel 1 exit /b 1

If args aren’t passed, the script opens dialogs.

Using env variables for defaults

  • Put OUTPUT_DIR, TOKEN, etc. in .env next to the script (or parent).
  • Scripts read os.getenv("OUTPUT_DIR") as a default.
  • .bat can also set variables before launching Python.

.env and Python example

# .env
OUTPUT_DIR=F:\Out
API_TOKEN=xxxxx

# Python (default from env)
p.add_argument("--output", default=os.getenv("OUTPUT_DIR", ""))