1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
"""Command line interface for dotenv-cli."""
# remove when we don't support py38 anymore
from __future__ import annotations
import argparse
import logging
from typing import NoReturn
from dotenv_cli import __VERSION__
from dotenv_cli.core import run_dotenv
logger = logging.getLogger(__name__)
def parse_args(args: list[str] | None = None) -> argparse.Namespace:
"""Parse arguments.
Paramters
---------
args
This if for debugging only.
Returns
-------
argparse.Namespace
"""
parser = argparse.ArgumentParser(
description=(
"dotenv executes a given command with environment variables "
"loaded from a .env file."
),
)
parser.add_argument(
"-e",
"--dotenv",
help=(
"alternative .env file; this parameter can be provided multiple "
"times and the .env files will be evaluated in order"
),
action="append",
default=[".env"],
)
parser.add_argument(
"command",
help="shell command to execute",
nargs=argparse.REMAINDER,
)
parser.add_argument(
"--version",
action="version",
version=__VERSION__,
)
parser.add_argument(
"-r",
"--replace",
action="store_true",
help=(
"completely replace all existing environment variables with the "
"ones loaded from the .env file"
)
)
return parser.parse_args(args)
def main() -> NoReturn | int:
"""Run dotenv.
This function parses sys.argv and runs dotenv.
Returns
-------
int
the return value
"""
args = parse_args()
# if alternative .env file is given, remove the default one
if len(args.dotenv) > 1:
args.dotenv = args.dotenv[1:]
if not args.command:
return 0
return run_dotenv(args.dotenv, args.command, args.replace)
|