33import sys
44import logging
55import argparse
6+ from decli import cli
67from pathlib import Path
78from configparser import RawConfigParser , NoSectionError
8- from commitizen import (
9- registered ,
10- run ,
11- set_commiter ,
12- show_example ,
13- show_info ,
14- show_schema ,
15- version ,
16- )
9+ from commitizen .application import Application
10+ from commitizen import deafults
1711
1812
1913logger = logging .getLogger (__name__ )
20-
21-
22- def get_parser (config ):
23- description = (
14+ data = {
15+ "prog" : "cz" ,
16+ "description" : (
2417 "Commitizen is a cli tool to generate conventional commits.\n "
2518 "For more information about the topic go to "
2619 "https://conventionalcommits.org/"
27- )
28-
29- formater = argparse .RawDescriptionHelpFormatter
30- parser = argparse .ArgumentParser (
31- prog = "cz" , description = description , formatter_class = formater
32- )
33- parser .set_defaults (func = run )
34- parser .add_argument (
35- "--debug" , action = "store_true" , default = False , help = "use debug mode"
36- )
37- parser .add_argument (
38- "-n" , "--name" , default = config .get ("name" ), help = "use the given commitizen"
39- )
40- parser .add_argument (
41- "--version" ,
42- action = "store_true" ,
43- default = False ,
44- help = "get the version of the installed commitizen" ,
45- )
46-
47- subparser = parser .add_subparsers (title = "commands" )
48-
49- lscz = subparser .add_parser ("ls" , help = "show available commitizens" )
50- lscz .set_defaults (func = registered )
51-
52- commit = subparser .add_parser ("commit" , aliases = ["c" ], help = "create new commit" )
53- commit .set_defaults (func = run )
54-
55- example = subparser .add_parser ("example" , help = "show commit example" )
56- example .set_defaults (func = show_example )
57-
58- info = subparser .add_parser ("info" , help = "show information about the cz" )
59- info .set_defaults (func = show_info )
60-
61- schema = subparser .add_parser ("schema" , help = "show commit schema" )
62- schema .set_defaults (func = show_schema )
63-
64- return parser
20+ ),
21+ "formatter_class" : argparse .RawDescriptionHelpFormatter ,
22+ "arguments" : [
23+ {"name" : "--debug" , "action" : "store_true" , "help" : "use debug mode" },
24+ {
25+ "name" : ["-n" , "--name" ],
26+ "default" : deafults .NAME ,
27+ "help" : "use the given commitizen" ,
28+ },
29+ {
30+ "name" : ["--version" ],
31+ "action" : "store_true" ,
32+ "help" : "get the version of the installed commitizen" ,
33+ },
34+ ],
35+ "subcommands" : {
36+ "title" : "commands" ,
37+ "commands" : [
38+ {
39+ "name" : "ls" ,
40+ "help" : "show available commitizens" ,
41+ "func" : lambda app : app .detected_cz ,
42+ },
43+ {
44+ "name" : ["commit" , "c" ],
45+ "help" : "create new commit" ,
46+ "func" : lambda app : app .cz .run ,
47+ },
48+ {
49+ "name" : "example" ,
50+ "help" : "show commit example" ,
51+ "func" : lambda app : app .cz .show_example ,
52+ },
53+ {
54+ "name" : "info" ,
55+ "help" : "show information about the cz" ,
56+ "func" : lambda app : app .cz .show_info ,
57+ },
58+ {
59+ "name" : "schema" ,
60+ "help" : "show commit schema" ,
61+ "func" : lambda app : app .cz .show_schema ,
62+ },
63+ ],
64+ },
65+ }
6566
6667
6768def load_cfg ():
68- defaults = {"name" : "cz_conventional_commits" }
69+ settings = {"name" : deafults . NAME }
6970 config = RawConfigParser ("" )
7071 home = str (Path .home ())
7172
@@ -86,26 +87,35 @@ def load_cfg():
8687 log_config = io .StringIO ()
8788 config .write (log_config )
8889 try :
89- defaults .update (dict (config .items ("commitizen" )))
90+ settings .update (dict (config .items ("commitizen" )))
9091 break
9192 except NoSectionError :
9293 # The file does not have commitizen section
9394 continue
9495
95- return defaults
96+ return settings
9697
9798
9899def main ():
99100 config = load_cfg ()
100- parser = get_parser (config )
101+ parser = cli (data )
102+
103+ # Show help if no arg provided
104+ if len (sys .argv ) == 1 :
105+ parser .print_help (sys .stderr )
106+ raise SystemExit (1 )
107+
101108 args = parser .parse_args ()
109+ app = Application (** config )
110+
111+ if args .name :
112+ app .name = args .name
102113
103114 if args .debug :
104115 logging .getLogger ("commitizen" ).setLevel (logging .DEBUG )
105116
106117 if args .version :
107- logger .info (version () )
118+ logger .info (app . version )
108119 sys .exit (0 )
109120
110- set_commiter (args .name )
111- args .func (args )
121+ args .func (app )(args )
0 commit comments