@@ -11,9 +11,11 @@ pub fn main() anyerror!void {
1111 const allocator = gpa .allocator ();
1212
1313 var app_context = AppContext {
14- .config_path = "" , // Will be populated by the root command's exec
14+ .config_path = "" ,
1515 };
1616
17+ defer if (app_context .config_path .len > 0 ) allocator .free (app_context .config_path );
18+
1719 const root_options = chilli.CommandOptions {
1820 .name = "chilli-app" ,
1921 .description = "A simple example CLI using Chilli." ,
@@ -32,7 +34,6 @@ pub fn main() anyerror!void {
3234 };
3335 try root_command .addFlag (config_flag );
3436
35- // --- `run` subcommand with variadic arguments ---
3637 const run_options = chilli.CommandOptions {
3738 .name = "run" ,
3839 .description = "Runs a task against a list of files." ,
@@ -41,14 +42,12 @@ pub fn main() anyerror!void {
4142 var run_command = try chilli .Command .init (allocator , run_options );
4243 try root_command .addSubcommand (run_command );
4344
44- // A required, typed positional argument
4545 try run_command .addPositional (.{
4646 .name = "task-name" ,
4747 .description = "The name of the task to run." ,
4848 .is_required = true ,
49- .type = .String , // Explicitly a string
49+ .type = .String ,
5050 });
51- // A variadic positional argument to capture all remaining values
5251 try run_command .addPositional (.{
5352 .name = "files" ,
5453 .description = "A list of files to process." ,
@@ -61,29 +60,50 @@ pub fn main() anyerror!void {
6160fn rootExec (ctx : chilli.CommandContext ) anyerror ! void {
6261 const app_ctx = ctx .getContextData (AppContext ).? ;
6362 const config_slice = try ctx .getFlag ("config" , []const u8 );
63+ const stdout = std .io .getStdOut ().writer ();
6464
65- // The slice from getFlag can point to temporary memory. To safely store it,
66- // it must be copied. The context's allocator is valid for this exec call.
67- app_ctx .config_path = try ctx .allocator .dupe (u8 , config_slice );
65+ if (app_ctx .config_path .len > 0 ) {
66+ ctx .app_allocator .free (app_ctx .config_path );
67+ }
68+ app_ctx .config_path = try ctx .app_allocator .dupe (u8 , config_slice );
6869
69- std . debug .print ("Welcome to chilli-app!\n " , .{});
70- std . debug .print (" Using config file: {s}\n\n " , .{app_ctx .config_path });
70+ try stdout .print ("Welcome to chilli-app!\n " , .{});
71+ try stdout .print (" Using config file: {s}\n\n " , .{app_ctx .config_path });
7172 try ctx .command .printHelp ();
7273}
7374
7475fn runExec (ctx : chilli.CommandContext ) anyerror ! void {
75- // Access positional arguments by name, now with type safety
7676 const task_name = try ctx .getArg ("task-name" , []const u8 );
77- const files = ctx .getArgs ("files" ); // Variadic arguments remain string slices
77+ const files = ctx .getArgs ("files" );
78+ const stdout = std .io .getStdOut ().writer ();
7879
79- std . debug .print ("Running task '{s}'...\n " , .{task_name });
80+ try stdout .print ("Running task '{s}'...\n " , .{task_name });
8081
8182 if (files .len == 0 ) {
82- std . debug .print ("No files provided to process.\n " , .{});
83+ try stdout .print ("No files provided to process.\n " , .{});
8384 } else {
84- std . debug .print ("Processing {d} files:\n " , .{files .len });
85+ try stdout .print ("Processing {d} files:\n " , .{files .len });
8586 for (files ) | file | {
86- std . debug .print (" - {s}\n " , .{file });
87+ try stdout .print (" - {s}\n " , .{file });
8788 }
8889 }
8990}
91+
92+ // Example Invocations
93+ //
94+ // 1. Build the example executable:
95+ // zig build e1_simple_cli
96+ //
97+ // 2. Run with different arguments:
98+ //
99+ // // Show the help output for the root command
100+ // ./zig-out/bin/e1_simple_cli --help
101+ //
102+ // // Run the 'run' subcommand with a task name and a list of files
103+ // ./zig-out/bin/e1_simple_cli run build-assets main.js styles.css script.js
104+ //
105+ // // Use the --config flag from the root command
106+ // ./zig-out/bin/e1_simple_cli --config ./custom.conf run process-logs
107+ //
108+ // // Use the environment variable to set the config path
109+ // CHILLI_APP_CONFIG=~/.config/chilli.conf ./zig-out/bin/e1_simple_cli run check-status
0 commit comments