11const std = @import ("std" );
22const cli = @import ("zig-cli" );
33
4- fn createAction (ctx : * cli.Command.ParseContext ) ! void {
5- const stdout = std .io .getStdOut ().writer ();
4+ fn createAction (ctx : * cli.BaseCommand.ParseContext ) ! void {
5+ var buf : [4096 ]u8 = undefined ;
6+ var file_writer = std .Io .File .stdout ().writerStreaming (std .Options .debug_io , & buf );
7+ const stdout = & file_writer .interface ;
68
79 const name = ctx .getArgument (0 ) orelse return error .MissingName ;
810 const template = ctx .getOption ("template" ) orelse "default" ;
@@ -11,10 +13,13 @@ fn createAction(ctx: *cli.Command.ParseContext) !void {
1113 try stdout .print ("Creating project: {s}\n " , .{name });
1214 try stdout .print ("Template: {s}\n " , .{template });
1315 try stdout .print ("Force: {s}\n " , .{if (force ) "yes" else "no" });
16+ try stdout .flush ();
1417}
1518
16- fn buildAction (ctx : * cli.Command.ParseContext ) ! void {
17- const stdout = std .io .getStdOut ().writer ();
19+ fn buildAction (ctx : * cli.BaseCommand.ParseContext ) ! void {
20+ var buf : [4096 ]u8 = undefined ;
21+ var file_writer = std .Io .File .stdout ().writerStreaming (std .Options .debug_io , & buf );
22+ const stdout = & file_writer .interface ;
1823
1924 const mode = ctx .getOption ("mode" ) orelse "debug" ;
2025 const optimize_str = ctx .getOption ("optimize" ) orelse "false" ;
@@ -29,10 +34,13 @@ fn buildAction(ctx: *cli.Command.ParseContext) !void {
2934 while (ctx .getArgument (i )) | target | : (i += 1 ) {
3035 try stdout .print ("Target: {s}\n " , .{target });
3136 }
37+ try stdout .flush ();
3238}
3339
34- fn testAction (ctx : * cli.Command.ParseContext ) ! void {
35- const stdout = std .io .getStdOut ().writer ();
40+ fn testAction (ctx : * cli.BaseCommand.ParseContext ) ! void {
41+ var buf : [4096 ]u8 = undefined ;
42+ var file_writer = std .Io .File .stdout ().writerStreaming (std .Options .debug_io , & buf );
43+ const stdout = & file_writer .interface ;
3644
3745 const filter = ctx .getOption ("filter" );
3846 const verbose = ctx .hasOption ("verbose" );
@@ -42,24 +50,25 @@ fn testAction(ctx: *cli.Command.ParseContext) !void {
4250 try stdout .print ("Filter: {s}\n " , .{f });
4351 }
4452 try stdout .print ("Verbose: {s}\n " , .{if (verbose ) "yes" else "no" });
53+ try stdout .flush ();
4554}
4655
47- pub fn main () ! void {
48- var gpa = std .heap .GeneralPurposeAllocator (.{}){};
49- defer _ = gpa .deinit ();
50- const allocator = gpa .allocator ();
56+ pub fn main (init : std.process.Init ) ! void {
57+ const allocator = init .gpa ;
5158
52- // Create CLI application
53- var app = try cli .CLI .init (
59+ // Create root command
60+ const root_cmd = try cli .BaseCommand .init (
5461 allocator ,
5562 "advanced-cli" ,
56- "2.0.0" ,
5763 "An advanced CLI application demonstrating complex features" ,
5864 );
59- defer app .deinit ();
65+ defer {
66+ root_cmd .deinit ();
67+ allocator .destroy (root_cmd );
68+ }
6069
6170 // Create 'create' command
62- const create_cmd = try cli .Command .init (allocator , "create" , "Create a new project" );
71+ const create_cmd = try cli .BaseCommand .init (allocator , "create" , "Create a new project" );
6372
6473 const create_name_arg = cli .Argument .init ("name" , "Project name" , .string );
6574 _ = try create_cmd .addArgument (create_name_arg );
@@ -74,10 +83,10 @@ pub fn main() !void {
7483 _ = try create_cmd .addOption (force_opt );
7584
7685 _ = create_cmd .setAction (createAction );
77- _ = try app . command (create_cmd );
86+ _ = try root_cmd . addCommand (create_cmd );
7887
7988 // Create 'build' command
80- const build_cmd = try cli .Command .init (allocator , "build" , "Build the project" );
89+ const build_cmd = try cli .BaseCommand .init (allocator , "build" , "Build the project" );
8190
8291 const mode_opt = cli .Option .init ("mode" , "mode" , "Build mode (debug/release)" , .string )
8392 .withShort ('m' )
@@ -94,10 +103,10 @@ pub fn main() !void {
94103 _ = try build_cmd .addArgument (targets_arg );
95104
96105 _ = build_cmd .setAction (buildAction );
97- _ = try app . command (build_cmd );
106+ _ = try root_cmd . addCommand (build_cmd );
98107
99108 // Create 'test' command
100- const test_cmd = try cli .Command .init (allocator , "test" , "Run tests" );
109+ const test_cmd = try cli .BaseCommand .init (allocator , "test" , "Run tests" );
101110
102111 const filter_opt = cli .Option .init ("filter" , "filter" , "Filter tests by name" , .string )
103112 .withShort ('f' );
@@ -108,11 +117,17 @@ pub fn main() !void {
108117 _ = try test_cmd .addOption (verbose_opt );
109118
110119 _ = test_cmd .setAction (testAction );
111- _ = try app .command (test_cmd );
112-
113- // Parse command line arguments
114- const args = try std .process .argsAlloc (allocator );
115- defer std .process .argsFree (allocator , args );
120+ _ = try root_cmd .addCommand (test_cmd );
121+
122+ // Collect command line arguments
123+ var args_list = std .ArrayList ([]const u8 ){};
124+ defer args_list .deinit (allocator );
125+ var args_iter = std .process .Args .Iterator .init (init .minimal .args );
126+ _ = args_iter .skip (); // skip program name
127+ while (args_iter .next ()) | arg | {
128+ try args_list .append (allocator , arg );
129+ }
116130
117- try app .parse (args );
131+ var parser = cli .Parser .init (allocator );
132+ try parser .parse (root_cmd , args_list .items );
118133}
0 commit comments