diff --git a/commitizen/cli.py b/commitizen/cli.py index 3cf75ce684..620707c588 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -6,7 +6,7 @@ from decli import cli from commitizen import commands, config, out -from commitizen.__version__ import __version__ + logger = logging.getLogger(__name__) data = { @@ -28,6 +28,8 @@ ], "subcommands": { "title": "commands", + # TODO: Add this constraint back in 2.0 + # "required": True, "commands": [ { "name": "ls", @@ -99,6 +101,11 @@ }, ], }, + { + "name": ["version"], + "help": "get the version of the installed commitizen", + "func": commands.Version, + }, ], }, } @@ -113,11 +120,22 @@ def main(): parser.print_help(sys.stderr) raise SystemExit() - args = parser.parse_args() + try: + args = parser.parse_args() + except TypeError: + out.error("Command is required") + raise SystemExit() if args.name: conf.update({"name": args.name}) + if args.version: + warnings.warn( + "'cz --version' will be deprecated in next major version. " + "Please use 'cz version' command from your scripts" + ) + logging.getLogger("commitizen").setLevel(logging.DEBUG) + if args.debug: warnings.warn( "Debug will be deprecated in next major version. " @@ -125,8 +143,8 @@ def main(): ) logging.getLogger("commitizen").setLevel(logging.DEBUG) - if args.version: - out.line(__version__) - raise SystemExit() - - args.func(conf, vars(args))() + # TODO: This try block can be removed after command is required in 2.0 + try: + args.func(conf, vars(args))() + except AttributeError: + out.error("Command is required") diff --git a/commitizen/commands/__init__.py b/commitizen/commands/__init__.py index 3343a53edc..644356a759 100644 --- a/commitizen/commands/__init__.py +++ b/commitizen/commands/__init__.py @@ -4,5 +4,6 @@ from .info import Info from .list_cz import ListCz from .schema import Schema +from .version import Version -__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema") +__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema", "Version") diff --git a/commitizen/commands/version.py b/commitizen/commands/version.py new file mode 100644 index 0000000000..b5158ef641 --- /dev/null +++ b/commitizen/commands/version.py @@ -0,0 +1,12 @@ +from commitizen import out +from commitizen.__version__ import __version__ + + +class Version: + """Get the version of the installed commitizen.""" + + def __init__(self, config: dict, *args): + self.config: dict = config + + def __call__(self): + out.write(__version__) diff --git a/tests/test_commands.py b/tests/test_commands.py index fbd8b3c777..249c046c2a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -134,3 +134,10 @@ def test_list_cz(): commands.ListCz(config)() mocked_write.assert_called_once() + + +def test_version(): + with mock.patch("commitizen.out.write") as mocked_write: + + commands.Version(config)() + mocked_write.assert_called_once()