@@ -11,70 +11,80 @@ pub fn init(allocator: std.mem.Allocator) Help {
1111
1212pub fn generate (self : * Help , command : * Command , cli_name : []const u8 , version : []const u8 ) ! void {
1313 _ = self ;
14- const stdout = std .io . getStdOut (). writer ();
14+ const stdout = std .fs . File . stdout ();
1515
1616 // Header
17- try stdout .print ("\n {s} v{s}\n " , .{ cli_name , version });
18- try stdout .print ("{s}\n\n " , .{command .description });
17+ var buf : [4096 ]u8 = undefined ;
18+ var line = try std .fmt .bufPrint (& buf , "\n {s} v{s}\n " , .{ cli_name , version });
19+ try stdout .writeAll (line );
20+ line = try std .fmt .bufPrint (& buf , "{s}\n\n " , .{command .description });
21+ try stdout .writeAll (line );
1922
2023 // Usage
21- try stdout .print ("USAGE:\n " , .{});
22- try stdout .print (" {s}" , .{command .name });
24+ try stdout .writeAll ("USAGE:\n " );
25+ line = try std .fmt .bufPrint (& buf , " {s}" , .{command .name });
26+ try stdout .writeAll (line );
2327
2428 if (command .options .items .len > 0 ) {
25- try stdout .print (" [OPTIONS]" , .{} );
29+ try stdout .writeAll (" [OPTIONS]" );
2630 }
2731
2832 if (command .subcommands .items .len > 0 ) {
29- try stdout .print (" <COMMAND>" , .{} );
33+ try stdout .writeAll (" <COMMAND>" );
3034 }
3135
3236 for (command .arguments .items ) | arg | {
3337 if (arg .required ) {
34- try stdout .print (" <{s}>" , .{arg .name });
38+ line = try std .fmt .bufPrint (& buf , " <{s}>" , .{arg .name });
39+ try stdout .writeAll (line );
3540 } else {
36- try stdout .print (" [{s}]" , .{arg .name });
41+ line = try std .fmt .bufPrint (& buf , " [{s}]" , .{arg .name });
42+ try stdout .writeAll (line );
3743 }
3844 if (arg .variadic ) {
39- try stdout .print ("..." , .{} );
45+ try stdout .writeAll ("..." );
4046 }
4147 }
42- try stdout .print ("\n\n " , .{} );
48+ try stdout .writeAll ("\n\n " );
4349
4450 // Arguments
4551 if (command .arguments .items .len > 0 ) {
46- try stdout .print ("ARGUMENTS:\n " , .{} );
52+ try stdout .writeAll ("ARGUMENTS:\n " );
4753 for (command .arguments .items ) | arg | {
48- try stdout .print (" <{s}>" , .{arg .name });
54+ line = try std .fmt .bufPrint (& buf , " <{s}>" , .{arg .name });
55+ try stdout .writeAll (line );
4956 if (arg .variadic ) {
50- try stdout .print ("..." , .{} );
57+ try stdout .writeAll ("..." );
5158 }
5259 const padding = 20 - | (arg .name .len + 2 + if (arg .variadic ) @as (usize , 3 ) else @as (usize , 0 ));
53- for (0.. padding ) | _ | try stdout .writeByte (' ' );
54- try stdout .print ("{s}" , .{arg .description });
60+ for (0.. padding ) | _ | try stdout .writeAll (" " );
61+ line = try std .fmt .bufPrint (& buf , "{s}" , .{arg .description });
62+ try stdout .writeAll (line );
5563 if (! arg .required ) {
56- try stdout .print (" (optional)" , .{} );
64+ try stdout .writeAll (" (optional)" );
5765 }
58- try stdout .print ("\n " , .{} );
66+ try stdout .writeAll ("\n " );
5967 }
60- try stdout .print ("\n " , .{} );
68+ try stdout .writeAll ("\n " );
6169 }
6270
6371 // Options
6472 if (command .options .items .len > 0 ) {
65- try stdout .print ("OPTIONS:\n " , .{} );
73+ try stdout .writeAll ("OPTIONS:\n " );
6674 for (command .options .items ) | opt | {
6775 var length : usize = 0 ;
6876
6977 if (opt .short ) | s | {
70- try stdout .print (" -{c}, " , .{s });
78+ line = try std .fmt .bufPrint (& buf , " -{c}, " , .{s });
79+ try stdout .writeAll (line );
7180 length += 6 ;
7281 } else {
73- try stdout .print (" " , .{} );
82+ try stdout .writeAll (" " );
7483 length += 6 ;
7584 }
7685
77- try stdout .print ("--{s}" , .{opt .long });
86+ line = try std .fmt .bufPrint (& buf , "--{s}" , .{opt .long });
87+ try stdout .writeAll (line );
7888 length += opt .long .len + 2 ;
7989
8090 if (opt .option_type != .bool ) {
@@ -84,40 +94,46 @@ pub fn generate(self: *Help, command: *Command, cli_name: []const u8, version: [
8494 .float = > "<FLOAT>" ,
8595 .bool = > "" ,
8696 };
87- try stdout .print (" {s}" , .{type_name });
97+ line = try std .fmt .bufPrint (& buf , " {s}" , .{type_name });
98+ try stdout .writeAll (line );
8899 length += type_name .len + 1 ;
89100 }
90101
91102 const padding = 30 - | length ;
92- for (0.. padding ) | _ | try stdout .writeByte ( ' ' );
103+ for (0.. padding ) | _ | try stdout .writeAll ( " " );
93104
94- try stdout .print ("{s}" , .{opt .description });
105+ line = try std .fmt .bufPrint (& buf , "{s}" , .{opt .description });
106+ try stdout .writeAll (line );
95107
96108 if (opt .required ) {
97- try stdout .print (" (required)" , .{} );
109+ try stdout .writeAll (" (required)" );
98110 } else if (opt .default_value ) | default | {
99- try stdout .print (" (default: {s})" , .{default });
111+ line = try std .fmt .bufPrint (& buf , " (default: {s})" , .{default });
112+ try stdout .writeAll (line );
100113 }
101114
102- try stdout .print ("\n " , .{} );
115+ try stdout .writeAll ("\n " );
103116 }
104117
105- try stdout .print (" -h, --help" , .{} );
106- for (0.. 20) | _ | try stdout .writeByte ( ' ' );
107- try stdout .print ("Print help\n " , .{} );
108- try stdout .print ("\n " , .{} );
118+ try stdout .writeAll (" -h, --help" );
119+ for (0.. 20) | _ | try stdout .writeAll ( " " );
120+ try stdout .writeAll ("Print help\n " );
121+ try stdout .writeAll ("\n " );
109122 }
110123
111124 // Commands
112125 if (command .subcommands .items .len > 0 ) {
113- try stdout .print ("COMMANDS:\n " , .{} );
126+ try stdout .writeAll ("COMMANDS:\n " );
114127 for (command .subcommands .items ) | subcmd | {
115- try stdout .print (" {s}" , .{subcmd .name });
128+ line = try std .fmt .bufPrint (& buf , " {s}" , .{subcmd .name });
129+ try stdout .writeAll (line );
116130 const padding = 20 - | subcmd .name .len ;
117- for (0.. padding ) | _ | try stdout .writeByte (' ' );
118- try stdout .print ("{s}\n " , .{subcmd .description });
131+ for (0.. padding ) | _ | try stdout .writeAll (" " );
132+ line = try std .fmt .bufPrint (& buf , "{s}\n " , .{subcmd .description });
133+ try stdout .writeAll (line );
119134 }
120- try stdout .print ("\n " , .{});
121- try stdout .print ("Run '{s} <COMMAND> --help' for more information on a command.\n\n " , .{command .name });
135+ try stdout .writeAll ("\n " );
136+ line = try std .fmt .bufPrint (& buf , "Run '{s} <COMMAND> --help' for more information on a command.\n\n " , .{command .name });
137+ try stdout .writeAll (line );
122138 }
123139}
0 commit comments