-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jepler/issue6
add `chap --version`
- Loading branch information
Showing
1 changed file
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] | ||
|
@@ -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() |