55import argparse
66import logging
77import sys
8+ import textwrap
9+ import typing as t
810
911import yaml
1012
3537INFO_SHORT_HELP = 'Get details on a CJK character, e.g. "好"'
3638
3739
40+ def build_description (
41+ intro : str ,
42+ example_blocks : t .Sequence [tuple [str | None , t .Sequence [str ]]],
43+ ) -> str :
44+ """Assemble help text with optional example sections.
45+
46+ Parameters
47+ ----------
48+ intro : str
49+ Introduction text to display at the top of the help.
50+ example_blocks : Sequence[tuple[str | None, Sequence[str]]]
51+ List of (heading, commands) tuples. If heading is None, displays
52+ as "examples:", otherwise as "{heading} examples:".
53+
54+ Returns
55+ -------
56+ str
57+ Formatted help text with examples.
58+ """
59+ sections : list [str ] = []
60+ intro_text = textwrap .dedent (intro ).strip ()
61+ if intro_text :
62+ sections .append (intro_text )
63+
64+ for heading , commands in example_blocks :
65+ if not commands :
66+ continue
67+ title = "examples:" if heading is None else f"{ heading } examples:"
68+ lines = [title ]
69+ lines .extend (f" { command } " for command in commands )
70+ sections .append ("\n " .join (lines ))
71+
72+ return "\n \n " .join (sections )
73+
74+
75+ CLI_DESCRIPTION = build_description (
76+ """
77+ cihai - CJK character lookup tool.
78+
79+ Look up Chinese, Japanese, and Korean characters using the Unihan database.
80+ """ ,
81+ (
82+ (
83+ "info" ,
84+ [
85+ "cihai info 好" ,
86+ "cihai info --all 好" ,
87+ ],
88+ ),
89+ (
90+ "reverse" ,
91+ [
92+ 'cihai reverse "good"' ,
93+ 'cihai reverse --all "library"' ,
94+ ],
95+ ),
96+ ),
97+ )
98+
99+ INFO_DESCRIPTION = build_description (
100+ """
101+ Get details on a CJK character.
102+ """ ,
103+ (
104+ (
105+ None ,
106+ [
107+ "cihai info 好" ,
108+ "cihai info 你" ,
109+ "cihai info --all 好" ,
110+ ],
111+ ),
112+ ),
113+ )
114+
115+ REVERSE_DESCRIPTION = build_description (
116+ """
117+ Search definitions for character matches.
118+ """ ,
119+ (
120+ (
121+ None ,
122+ [
123+ 'cihai reverse "good"' ,
124+ 'cihai reverse "library"' ,
125+ 'cihai reverse --all "love"' ,
126+ ],
127+ ),
128+ ),
129+ )
130+
131+
38132def create_parser () -> argparse .ArgumentParser :
39133 """Create argparse for cihai-cli."""
40- parser = argparse .ArgumentParser (prog = "cihai" )
134+ parser = argparse .ArgumentParser (
135+ prog = "cihai" ,
136+ description = CLI_DESCRIPTION ,
137+ formatter_class = argparse .RawDescriptionHelpFormatter ,
138+ )
41139 parser .add_argument (
42140 "--version" ,
43141 "-V" ,
@@ -61,11 +159,18 @@ def create_parser() -> argparse.ArgumentParser:
61159 help = "Path to custom config file" ,
62160 )
63161 subparsers = parser .add_subparsers (dest = "subparser_name" )
64- info_parser = subparsers .add_parser ("info" , help = INFO_SHORT_HELP )
162+ info_parser = subparsers .add_parser (
163+ "info" ,
164+ help = INFO_SHORT_HELP ,
165+ description = INFO_DESCRIPTION ,
166+ formatter_class = argparse .RawDescriptionHelpFormatter ,
167+ )
65168 create_info_subparser (info_parser )
66169 reverse_parser = subparsers .add_parser (
67170 "reverse" ,
68171 help = 'Search all info for character matches, e.g. "good"' ,
172+ description = REVERSE_DESCRIPTION ,
173+ formatter_class = argparse .RawDescriptionHelpFormatter ,
69174 )
70175 create_reverse_subparser (reverse_parser )
71176
0 commit comments