1+ <?php
2+
3+ namespace Crypt \Console ;
4+
5+
6+ require_once __DIR__ . '/PinCliParameters.php ' ;
7+
8+ class SimpleCliWrapper
9+ {
10+ const DEFAULT_VERBOSITY = 0 ;
11+ const INVALID_INPUT = -1 ;
12+ const VERBOSE_LONG = 'verbose ' ;
13+ const VERBOSE_SHORT = 'v ' ;
14+ const HELP_SHORT = 'h ' ;
15+ const HELP_LONG = 'help ' ;
16+ const LOG_SHORT = 'l ' ;
17+ const LOG_LONG = 'log ' ;
18+ const OPTIONAL_INDICATOR = ':: ' ;
19+
20+ /**
21+ * The old definition for the CLI options was:
22+ * ```xml
23+ * <description>Utility that emulates GnuPG 1.x passphrase handling over pipe-based IPC for GnuPG 2.x.</description>
24+ * <version>@package-version@</version>
25+ * <option name="log">
26+ * <short_name>-l</short_name>
27+ * <long_name>--log</long_name>
28+ * <description>Optional location to log pinentry activity.</description>
29+ * <action>StoreString</action>
30+ * </option>
31+ * <option name="verbose">
32+ * <short_name>-v</short_name>
33+ * <long_name>--verbose</long_name>
34+ * <description>Sets verbosity level. Use multiples for more detail (e.g. "-vv").</description>
35+ * <action>Counter</action>
36+ * <default>0</default>
37+ * </option>
38+ * </description>
39+ * ```
40+ *
41+ * @return PinCliParameters
42+ */
43+ public function parseCli ()
44+ {
45+ $ shortOpts = implode ('' , [
46+ self ::VERBOSE_SHORT . self ::OPTIONAL_INDICATOR ,
47+ self ::LOG_SHORT . self ::OPTIONAL_INDICATOR ,
48+ self ::HELP_SHORT . self ::OPTIONAL_INDICATOR
49+ ]);
50+
51+ $ longOpts = [
52+ self ::VERBOSE_LONG . self ::OPTIONAL_INDICATOR ,
53+ self ::LOG_LONG . self ::OPTIONAL_INDICATOR ,
54+ self ::HELP_LONG . self ::OPTIONAL_INDICATOR
55+ ];
56+
57+ $ opts = getopt ($ shortOpts , $ longOpts );
58+ if (isset ($ opts [self ::HELP_SHORT ]) || isset ($ opts [self ::HELP_LONG ])) {
59+ $ this ->printHelp ();
60+ exit (1 );
61+ }
62+
63+ $ verbosityLevel = self ::getVerbosityLevel ($ opts );
64+ if ($ verbosityLevel === self ::INVALID_INPUT ) {
65+ $ this ->writeToErrOrEcho ("Invalid verbosity level. Please use -h or --help. \n" );
66+ exit (1 );
67+ }
68+
69+ $ logLocation = self ::getLogLocation ($ opts );
70+ if ($ logLocation === self ::INVALID_INPUT ) {
71+ $ this ->writeToErrOrEcho ("Invalid log location. Please use -h or --help. \n" );
72+ exit (1 );
73+ }
74+
75+ return new PinCliParameters (
76+ $ verbosityLevel ,
77+ $ logLocation
78+ );
79+ }
80+
81+ /**
82+ * replication of previous behavior from PEAR Console_CommandLine
83+ * which is abandoned now.
84+ *
85+ * ```
86+ * public function stderr($msg)
87+ * {
88+ * if (defined('STDERR')) {
89+ * fwrite(STDERR, $msg);
90+ * } else {
91+ * echo $msg;
92+ * }
93+ * }
94+ * ```
95+ *
96+ * @param $msg
97+ * @return void
98+ */
99+ public function writeToErrOrEcho ($ msg )
100+ {
101+ if (defined ('STDERR ' )) {
102+ fwrite (STDERR , $ msg );
103+ } else {
104+ echo $ msg ;
105+ }
106+ }
107+
108+ private function printHelp ()
109+ {
110+ echo "
111+ Utility that emulates GnuPG 1.x passphrase handling over pipe-based IPC for GnuPG 2.x.
112+
113+ Options:
114+ -h, --help: Display this help message.
115+ -l, --log: Optional location to log pinentry activity.
116+ -v, --verbose: Verbosity level for logging.
117+
118+ The default verbosity level is 0.
119+ Increase verbosity levels for more detail:
120+ Short Syntax: -vvv
121+ Long Syntax: --verbose 3
122+
123+ Set the Log Location:
124+ Short Syntax: -l/path/to/log/file
125+ Long Syntax: --log /path/to/log/file
126+
127+ the short syntax will be taken before the long syntax.
128+ " ;
129+ }
130+
131+
132+ /**
133+ * @param array $opts
134+ *
135+ * @return int
136+ */
137+ public static function getVerbosityLevel (array $ opts )
138+ {
139+ if (!isset ($ opts [self ::VERBOSE_SHORT ]) && !isset ($ opts [self ::VERBOSE_LONG ])) {
140+ return self ::DEFAULT_VERBOSITY ;
141+ }
142+
143+
144+ // the default options with just a -v is false, but based
145+ // on the old system, it would be level 0
146+ if (isset ($ opts [self ::VERBOSE_SHORT ])) {
147+ if ($ opts [self ::VERBOSE_SHORT ] === false ) {
148+ return self ::DEFAULT_VERBOSITY ;
149+ }
150+
151+ // the first v will be stripped -v so we count the amounts of v's after that
152+ return (int )mb_strlen ($ opts [self ::VERBOSE_SHORT ]);
153+ }
154+
155+ if (isset ($ opts [self ::VERBOSE_LONG ]) && !is_numeric ($ opts [self ::VERBOSE_LONG ])) {
156+ return self ::INVALID_INPUT ;
157+ }
158+
159+ if (isset ($ opts [self ::VERBOSE_LONG ])) {
160+ return (int )$ opts [self ::VERBOSE_LONG ];
161+ }
162+
163+ return self ::INVALID_INPUT ;
164+
165+ }
166+
167+
168+ public static function getLogLocation (array $ opts )
169+ {
170+ if (!isset ($ opts [self ::LOG_SHORT ]) && !isset ($ opts [self ::LOG_LONG ])) {
171+ return "" ;
172+ }
173+
174+ if (isset ($ opts [self ::LOG_SHORT ]) && is_string ($ opts [self ::LOG_SHORT ])) {
175+ return $ opts [self ::LOG_SHORT ];
176+ }
177+
178+ if (isset ($ opts [self ::LOG_LONG ]) && is_string ($ opts [self ::LOG_LONG ])) {
179+ return $ opts [self ::LOG_LONG ];
180+ }
181+
182+ return self ::INVALID_INPUT ;
183+ }
184+ }
0 commit comments