Skip to content

Commit

Permalink
Merge pull request #7 from jepler/issue6
Browse files Browse the repository at this point in the history
add `chap --version`
  • Loading branch information
jepler authored May 20, 2023
2 parents 4c91072 + eab73bd commit 1c597cd
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/chap/__main__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
# SPDX-FileCopyrightText: 2023 Jeff Epler <[email protected]>
#
# SPDX-License-Identifier: MIT
# pylint: disable=import-outside-toplevel

import importlib
import pathlib
import pkgutil
import subprocess

import click

from . import commands # pylint: disable=no-name-in-module


def version_callback(ctx, param, value) -> None: # pylint: disable=unused-argument
if not value or ctx.resilient_parsing:
return

git_dir = pathlib.Path(__file__).parent.parent.parent / ".git"
if git_dir.exists():
version = subprocess.check_output(
["git", f"--git-dir={git_dir}", "describe", "--tags", "--dirty"],
encoding="utf-8",
)
else:
try:
from .__version__ import __version__ as version
except ImportError:
version = "unknown"
prog_name = ctx.find_root().info_name
click.utils.echo(
f"{prog_name}, version {version}",
color=ctx.color,
)
ctx.exit()


class MyCLI(click.MultiCommand):
def list_commands(self, ctx):
rv = []
Expand All @@ -27,7 +53,18 @@ def get_command(self, ctx, cmd_name):
raise click.UsageError(f"Invalid subcommand {cmd_name!r}", ctx) from exc


main = MyCLI(help="Commandline interface to ChatGPT")
main = MyCLI(
help="Commandline interface to ChatGPT",
params=[
click.Option(
("--version",),
is_flag=True,
is_eager=True,
help="Show the version and exit",
callback=version_callback,
)
],
)

if __name__ == "__main__":
main()

0 comments on commit 1c597cd

Please sign in to comment.