1212 * -----------------------------------------------------------------------------
1313 */
1414#include "fossil/io/parser.h"
15+ #include "fossil/io/output.h"
1516#include <stdio.h>
1617#include <stdlib.h>
1718#include <limits.h>
@@ -76,36 +77,33 @@ void show_help(const char *command_name, const fossil_io_parser_palette_t *palet
7677
7778 // If no specific command is provided, show all commands
7879 if (!command_name ) {
79- printf ( " Available commands:\n" );
80+ fossil_io_printf ( "{blue} Available commands:{reset} \n" );
8081 while (command ) {
81- printf ( " %s: %s\n" , command -> name , command -> description );
82+ fossil_io_printf ( "{cyan} %s: %s{reset} \n" , command -> name , command -> description );
8283 command = command -> next ;
8384 }
84- printf ("\nUse '--help <command>' for details on a specific command.\n" );
85+ fossil_io_printf ("\n{blue}Use '--help <command>' for details on a specific command.{reset} \n" );
8586 return ;
8687 }
8788
8889 // Search for the specific command
8990 while (command ) {
9091 if (strcmp (command -> name , command_name ) == 0 ) {
91- printf ( " Command: %s\nDescription: %s\n" , command -> name , command -> description );
92- printf ( " Arguments:\n" );
92+ fossil_io_printf ( "{blue} Command: %s\nDescription: %s{reset} \n" , command -> name , command -> description );
93+ fossil_io_printf ( "{blue} Arguments:{reset} \n" );
9394 fossil_io_parser_argument_t * arg = command -> arguments ;
9495 while (arg ) {
95- printf ( " --%s (%s): %s\n" ,
96+ fossil_io_printf ( "{cyan} --%s (%s): %s{reset} \n" ,
9697 arg -> name ,
9798 arg -> type == FOSSIL_IO_PARSER_BOOL ? "bool" :
9899 arg -> type == FOSSIL_IO_PARSER_STRING ? "string" :
99100 arg -> type == FOSSIL_IO_PARSER_INT ? "int" :
100- "combo" ,
101+ arg -> type == FOSSIL_IO_PARSER_FLOAT ? "float" :
102+ arg -> type == FOSSIL_IO_PARSER_DATE ? "date" :
103+ arg -> type == FOSSIL_IO_PARSER_ARRAY ? "array" :
104+ arg -> type == FOSSIL_IO_PARSER_FEATURE ? "feature" :
105+ "unknown" ,
101106 arg -> value ? arg -> value : "No default value" );
102- if (arg -> type == FOSSIL_IO_PARSER_COMBO ) {
103- printf (" Options: " );
104- for (int i = 0 ; i < arg -> combo_count ; i ++ ) {
105- printf ("%s%s" , arg -> combo_options [i ], i == arg -> combo_count - 1 ? "" : ", " );
106- }
107- printf ("\n" );
108- }
109107 arg = arg -> next ;
110108 }
111109 return ;
@@ -114,41 +112,57 @@ void show_help(const char *command_name, const fossil_io_parser_palette_t *palet
114112 }
115113
116114 // If the command is not found
117- fprintf ( stderr , "Unknown command '%s'. Use '--help' to see available commands.\n" , command_name );
115+ fossil_io_fprintf ( FOSSIL_STDERR , "{red} Unknown command '%s'. Use '--help' to see available commands.{reset} \n" , command_name );
118116}
119117
120-
121118void show_usage (const char * command_name , const fossil_io_parser_palette_t * palette ) {
122119 fossil_io_parser_command_t * command = palette -> commands ;
123120
124121 // Search for the specific command
125122 while (command ) {
126123 if (strcmp (command -> name , command_name ) == 0 ) {
127- printf ( " Usage example for '%s':\n" , command -> name );
128- printf ( " %s" , command -> name );
124+ fossil_io_printf ( "{blue} Usage example for '%s':{reset} \n" , command -> name );
125+ fossil_io_printf ( "{cyan} %s{reset} " , command -> name );
129126
130127 fossil_io_parser_argument_t * arg = command -> arguments ;
131128 while (arg ) {
132- printf (" --%s " , arg -> name );
133- if (arg -> type == FOSSIL_IO_PARSER_STRING ) {
134- printf ("<string>" );
135- } else if (arg -> type == FOSSIL_IO_PARSER_INT ) {
136- printf ("<int>" );
137- } else if (arg -> type == FOSSIL_IO_PARSER_BOOL ) {
138- printf ("<true/false>" );
139- } else if (arg -> type == FOSSIL_IO_PARSER_COMBO ) {
140- printf ("<%s>" , arg -> combo_options [0 ]); // Show first combo option
129+ fossil_io_printf ("{cyan} --%s {reset}" , arg -> name );
130+ switch (arg -> type ) {
131+ case FOSSIL_IO_PARSER_STRING :
132+ fossil_io_printf ("{cyan}<string>{reset}" );
133+ break ;
134+ case FOSSIL_IO_PARSER_INT :
135+ fossil_io_printf ("{cyan}<int>{reset}" );
136+ break ;
137+ case FOSSIL_IO_PARSER_BOOL :
138+ fossil_io_printf ("{cyan}<true/false>{reset}" );
139+ break ;
140+ case FOSSIL_IO_PARSER_FLOAT :
141+ fossil_io_printf ("{cyan}<float>{reset}" );
142+ break ;
143+ case FOSSIL_IO_PARSER_DATE :
144+ fossil_io_printf ("{cyan}<YYYY-MM-DD>{reset}" );
145+ break ;
146+ case FOSSIL_IO_PARSER_ARRAY :
147+ fossil_io_printf ("{cyan}<value1,value2,...>{reset}" );
148+ break ;
149+ case FOSSIL_IO_PARSER_FEATURE :
150+ fossil_io_printf ("{cyan}<enable/disable>{reset}" );
151+ break ;
152+ default :
153+ fossil_io_printf ("{cyan}<unknown>{reset}" );
154+ break ;
141155 }
142156 arg = arg -> next ;
143157 }
144- printf ("\n" );
158+ fossil_io_printf ("\n" );
145159 return ;
146160 }
147161 command = command -> next ;
148162 }
149163
150164 // If the command is not found
151- fprintf ( stderr , "Unknown command '%s'. Use '--help' to see available commands.\n" , command_name );
165+ fossil_io_fprintf ( FOSSIL_STDERR , "{red} Unknown command '%s'. Use '--help' to see available commands.{reset} \n" , command_name );
152166}
153167
154168fossil_io_parser_palette_t * fossil_io_parser_create_palette (const char * name , const char * description ) {
@@ -188,7 +202,7 @@ fossil_io_parser_argument_t *fossil_io_parser_add_argument(fossil_io_parser_comm
188202// Updated parse function
189203void fossil_io_parser_parse (fossil_io_parser_palette_t * palette , int argc , char * * argv ) {
190204 if (argc < 2 ) {
191- fprintf ( stderr , "No command provided.\n" );
205+ fossil_io_fprintf ( FOSSIL_STDERR , "{red} No command provided.{reset} \n" );
192206 return ;
193207 }
194208
@@ -208,7 +222,7 @@ void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char
208222 if (argc == 3 ) {
209223 show_usage (argv [2 ], palette ); // Show usage for a specific command
210224 } else {
211- fprintf ( stderr , "Usage: --usage <command>\n" );
225+ fossil_io_fprintf ( FOSSIL_STDERR , "{blue} Usage:{cuan} --usage <command>{reset} \n" );
212226 }
213227 return ;
214228 }
@@ -225,9 +239,9 @@ void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char
225239 // Suggest a similar command or show an error
226240 const char * suggestion = suggest_command (command_name , palette );
227241 if (suggestion ) {
228- fprintf ( stderr , "Unknown command: '%s'. Did you mean '%s'?\n" , command_name , suggestion );
242+ fossil_io_fprintf ( FOSSIL_STDERR , "{red} Unknown command: '%s'. Did you mean '%s'?{reset} \n" , command_name , suggestion );
229243 } else {
230- fprintf ( stderr , "Unknown command: '%s'. Type '--help' to see available commands.\n" , command_name );
244+ fossil_io_fprintf ( FOSSIL_STDERR , "{red} Unknown command: '%s'. Type '--help' to see available commands.{reset} \n" , command_name );
231245 }
232246 return ;
233247 }
@@ -241,31 +255,60 @@ void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char
241255 switch (argument -> type ) {
242256 case FOSSIL_IO_PARSER_BOOL :
243257 argument -> value = malloc (sizeof (int ));
244- if (strcmp (arg_value , "enable " ) == 0 ) {
258+ if (strcmp (arg_value , "true" ) == 0 || strcmp ( arg_value , "yes " ) == 0 ) {
245259 * (int * )argument -> value = 1 ; // Enable
246- } else if (strcmp (arg_value , "disable " ) == 0 ) {
260+ } else if (strcmp (arg_value , "false" ) == 0 || strcmp ( arg_value , "no " ) == 0 ) {
247261 * (int * )argument -> value = 0 ; // Disable
248262 } else {
249- fprintf (stderr , "Invalid value for boolean argument: %s\n" , arg_value );
250- free (argument -> value );
263+ fossil_io_fprintf (FOSSIL_STDERR , "{red}Invalid value for boolean argument: %s{reset}\n" , arg_value );
251264 argument -> value = NULL ;
252265 }
253266 break ;
254267 case FOSSIL_IO_PARSER_STRING :
255- argument -> value = _custom_strdup (arg_value ); // Custom _custom_strdup
268+ argument -> value = _custom_strdup (arg_value );
256269 break ;
257270 case FOSSIL_IO_PARSER_INT :
258271 argument -> value = malloc (sizeof (int ));
259272 * (int * )argument -> value = atoi (arg_value );
260273 break ;
261- case FOSSIL_IO_PARSER_COMBO :
262- for (int j = 0 ; j < argument -> combo_count ; j ++ ) {
263- if (strcmp (arg_value , argument -> combo_options [j ]) == 0 ) {
264- argument -> value = _custom_strdup (arg_value );
265- break ;
266- }
274+ case FOSSIL_IO_PARSER_FLOAT :
275+ argument -> value = malloc (sizeof (float ));
276+ * (float * )argument -> value = atof (arg_value );
277+ break ;
278+ case FOSSIL_IO_PARSER_DATE :
279+ argument -> value = _custom_strdup (arg_value ); // Assume valid date format
280+ break ;
281+ case FOSSIL_IO_PARSER_ARRAY : {
282+ char * array_copy = _custom_strdup (arg_value );
283+ char * token = strtok (array_copy , "," );
284+ char * * array_values = NULL ;
285+ int count = 0 ;
286+
287+ while (token ) {
288+ array_values = realloc (array_values , sizeof (char * ) * (count + 1 ));
289+ array_values [count ++ ] = _custom_strdup (token );
290+ token = strtok (NULL , "," );
291+ }
292+
293+ argument -> value = (char * )array_values ;
294+ break ;
295+ }
296+ case FOSSIL_IO_PARSER_FEATURE :
297+ argument -> value = malloc (sizeof (int ));
298+ if (strcmp (arg_value , "enable" ) == 0 ) {
299+ * (int * )argument -> value = 1 ; // Enable
300+ } else if (strcmp (arg_value , "disable" ) == 0 ) {
301+ * (int * )argument -> value = 0 ; // Disable
302+ } else if (strcmp (arg_value , "auto" ) == 0 ) {
303+ * (int * )argument -> value = 2 ; // Auto
304+ } else {
305+ fossil_io_fprintf (FOSSIL_STDERR , "{red}Invalid value for feature argument: %s{reset}\n" , arg_value );
306+ argument -> value = NULL ;
267307 }
268308 break ;
309+ default :
310+ fossil_io_fprintf (FOSSIL_STDERR , "{red}Unknown argument type for: %s{reset}\n" , arg_value );
311+ break ;
269312 }
270313 break ;
271314 }
@@ -279,11 +322,10 @@ void fossil_io_parser_free(fossil_io_parser_palette_t *palette) {
279322 while (command ) {
280323 fossil_io_parser_argument_t * argument = command -> arguments ;
281324 while (argument ) {
282- if (argument -> type == FOSSIL_IO_PARSER_COMBO ) {
283- free (argument -> combo_options );
284- }
285325 free (argument -> name );
286- free (argument -> value );
326+ if (argument -> value && argument -> value != (char * )argument -> combo_options ) {
327+ free (argument -> value );
328+ }
287329 argument = argument -> next ;
288330 }
289331 free (command -> name );
0 commit comments