Skip to content

Commit c100cf7

Browse files
authored
Merge pull request #48 from Lee-W/make-command-required
Make command required
2 parents 976573e + 8ae5d8b commit c100cf7

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

commitizen/cli.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from decli import cli
77

88
from commitizen import commands, config, out
9-
from commitizen.__version__ import __version__
9+
1010

1111
logger = logging.getLogger(__name__)
1212
data = {
@@ -28,6 +28,8 @@
2828
],
2929
"subcommands": {
3030
"title": "commands",
31+
# TODO: Add this constraint back in 2.0
32+
# "required": True,
3133
"commands": [
3234
{
3335
"name": "ls",
@@ -99,6 +101,11 @@
99101
},
100102
],
101103
},
104+
{
105+
"name": ["version"],
106+
"help": "get the version of the installed commitizen",
107+
"func": commands.Version,
108+
},
102109
],
103110
},
104111
}
@@ -113,20 +120,31 @@ def main():
113120
parser.print_help(sys.stderr)
114121
raise SystemExit()
115122

116-
args = parser.parse_args()
123+
try:
124+
args = parser.parse_args()
125+
except TypeError:
126+
out.error("Command is required")
127+
raise SystemExit()
117128

118129
if args.name:
119130
conf.update({"name": args.name})
120131

132+
if args.version:
133+
warnings.warn(
134+
"'cz --version' will be deprecated in next major version. "
135+
"Please use 'cz version' command from your scripts"
136+
)
137+
logging.getLogger("commitizen").setLevel(logging.DEBUG)
138+
121139
if args.debug:
122140
warnings.warn(
123141
"Debug will be deprecated in next major version. "
124142
"Please remove it from your scripts"
125143
)
126144
logging.getLogger("commitizen").setLevel(logging.DEBUG)
127145

128-
if args.version:
129-
out.line(__version__)
130-
raise SystemExit()
131-
132-
args.func(conf, vars(args))()
146+
# TODO: This try block can be removed after command is required in 2.0
147+
try:
148+
args.func(conf, vars(args))()
149+
except AttributeError:
150+
out.error("Command is required")

commitizen/commands/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
from .info import Info
55
from .list_cz import ListCz
66
from .schema import Schema
7+
from .version import Version
78

8-
__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema")
9+
__all__ = ("Bump", "Commit", "Example", "Info", "ListCz", "Schema", "Version")

commitizen/commands/version.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from commitizen import out
2+
from commitizen.__version__ import __version__
3+
4+
5+
class Version:
6+
"""Get the version of the installed commitizen."""
7+
8+
def __init__(self, config: dict, *args):
9+
self.config: dict = config
10+
11+
def __call__(self):
12+
out.write(__version__)

tests/test_commands.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,10 @@ def test_list_cz():
134134

135135
commands.ListCz(config)()
136136
mocked_write.assert_called_once()
137+
138+
139+
def test_version():
140+
with mock.patch("commitizen.out.write") as mocked_write:
141+
142+
commands.Version(config)()
143+
mocked_write.assert_called_once()

0 commit comments

Comments
 (0)