@@ -9,82 +9,81 @@ pub fn init(allocator: std.mem.Allocator) Help {
99 return .{ .allocator = allocator };
1010}
1111
12+ fn getWriter () std.Io.File.Writer {
13+ const io = std .Options .debug_io ;
14+ var buf : [4096 ]u8 = undefined ;
15+ return std .Io .File .stdout ().writerStreaming (io , & buf );
16+ }
17+
1218pub fn generate (self : * Help , command : * Command , cli_name : []const u8 , version : []const u8 ) ! void {
1319 _ = self ;
14- const stdout = std .fs .File .stdout ();
20+ var buf : [4096 ]u8 = undefined ;
21+ const io = std .Options .debug_io ;
22+ var file_writer = std .Io .File .stdout ().writerStreaming (io , & buf );
23+ const stdout = & file_writer .interface ;
1524
1625 // Header
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 );
26+ try stdout .print ("\n {s} v{s}\n " , .{ cli_name , version });
27+ try stdout .print ("{s}\n\n " , .{command .description });
2228
2329 // Usage
24- try stdout .writeAll ("USAGE:\n " );
25- line = try std .fmt .bufPrint (& buf , " {s}" , .{command .name });
26- try stdout .writeAll (line );
30+ try stdout .print ("USAGE:\n " , .{});
31+ try stdout .print (" {s}" , .{command .name });
2732
2833 if (command .options .items .len > 0 ) {
29- try stdout .writeAll (" [OPTIONS]" );
34+ try stdout .print (" [OPTIONS]" , .{} );
3035 }
3136
3237 if (command .subcommands .items .len > 0 ) {
33- try stdout .writeAll (" <COMMAND>" );
38+ try stdout .print (" <COMMAND>" , .{} );
3439 }
3540
3641 for (command .arguments .items ) | arg | {
3742 if (arg .required ) {
38- line = try std .fmt .bufPrint (& buf , " <{s}>" , .{arg .name });
39- try stdout .writeAll (line );
43+ try stdout .print (" <{s}>" , .{arg .name });
4044 } else {
41- line = try std .fmt .bufPrint (& buf , " [{s}]" , .{arg .name });
42- try stdout .writeAll (line );
45+ try stdout .print (" [{s}]" , .{arg .name });
4346 }
4447 if (arg .variadic ) {
45- try stdout .writeAll ("..." );
48+ try stdout .print ("..." , .{} );
4649 }
4750 }
48- try stdout .writeAll ("\n\n " );
51+ try stdout .print ("\n\n " , .{} );
4952
5053 // Arguments
5154 if (command .arguments .items .len > 0 ) {
52- try stdout .writeAll ("ARGUMENTS:\n " );
55+ try stdout .print ("ARGUMENTS:\n " , .{} );
5356 for (command .arguments .items ) | arg | {
54- line = try std .fmt .bufPrint (& buf , " <{s}>" , .{arg .name });
55- try stdout .writeAll (line );
57+ try stdout .print (" <{s}>" , .{arg .name });
5658 if (arg .variadic ) {
57- try stdout .writeAll ("..." );
59+ try stdout .print ("..." , .{} );
5860 }
5961 const padding = 20 - | (arg .name .len + 2 + if (arg .variadic ) @as (usize , 3 ) else @as (usize , 0 ));
60- for (0.. padding ) | _ | try stdout .writeAll (" " );
61- line = try std .fmt .bufPrint (& buf , "{s}" , .{arg .description });
62- try stdout .writeAll (line );
62+ for (0.. padding ) | _ | try stdout .writeByte (' ' );
63+ try stdout .print ("{s}" , .{arg .description });
6364 if (! arg .required ) {
64- try stdout .writeAll (" (optional)" );
65+ try stdout .print (" (optional)" , .{} );
6566 }
66- try stdout .writeAll ("\n " );
67+ try stdout .print ("\n " , .{} );
6768 }
68- try stdout .writeAll ("\n " );
69+ try stdout .print ("\n " , .{} );
6970 }
7071
7172 // Options
7273 if (command .options .items .len > 0 ) {
73- try stdout .writeAll ("OPTIONS:\n " );
74+ try stdout .print ("OPTIONS:\n " , .{} );
7475 for (command .options .items ) | opt | {
7576 var length : usize = 0 ;
7677
7778 if (opt .short ) | s | {
78- line = try std .fmt .bufPrint (& buf , " -{c}, " , .{s });
79- try stdout .writeAll (line );
79+ try stdout .print (" -{c}, " , .{s });
8080 length += 6 ;
8181 } else {
82- try stdout .writeAll (" " );
82+ try stdout .print (" " , .{} );
8383 length += 6 ;
8484 }
8585
86- line = try std .fmt .bufPrint (& buf , "--{s}" , .{opt .long });
87- try stdout .writeAll (line );
86+ try stdout .print ("--{s}" , .{opt .long });
8887 length += opt .long .len + 2 ;
8988
9089 if (opt .option_type != .bool ) {
@@ -94,46 +93,42 @@ pub fn generate(self: *Help, command: *Command, cli_name: []const u8, version: [
9493 .float = > "<FLOAT>" ,
9594 .bool = > "" ,
9695 };
97- line = try std .fmt .bufPrint (& buf , " {s}" , .{type_name });
98- try stdout .writeAll (line );
96+ try stdout .print (" {s}" , .{type_name });
9997 length += type_name .len + 1 ;
10098 }
10199
102100 const padding = 30 - | length ;
103- for (0.. padding ) | _ | try stdout .writeAll ( " " );
101+ for (0.. padding ) | _ | try stdout .writeByte ( ' ' );
104102
105- line = try std .fmt .bufPrint (& buf , "{s}" , .{opt .description });
106- try stdout .writeAll (line );
103+ try stdout .print ("{s}" , .{opt .description });
107104
108105 if (opt .required ) {
109- try stdout .writeAll (" (required)" );
106+ try stdout .print (" (required)" , .{} );
110107 } else if (opt .default_value ) | default | {
111- line = try std .fmt .bufPrint (& buf , " (default: {s})" , .{default });
112- try stdout .writeAll (line );
108+ try stdout .print (" (default: {s})" , .{default });
113109 }
114110
115- try stdout .writeAll ("\n " );
111+ try stdout .print ("\n " , .{} );
116112 }
117113
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 " );
114+ try stdout .print (" -h, --help" , .{} );
115+ for (0.. 20) | _ | try stdout .writeByte ( ' ' );
116+ try stdout .print ("Print help\n " , .{} );
117+ try stdout .print ("\n " , .{} );
122118 }
123119
124120 // Commands
125121 if (command .subcommands .items .len > 0 ) {
126- try stdout .writeAll ("COMMANDS:\n " );
122+ try stdout .print ("COMMANDS:\n " , .{} );
127123 for (command .subcommands .items ) | subcmd | {
128- line = try std .fmt .bufPrint (& buf , " {s}" , .{subcmd .name });
129- try stdout .writeAll (line );
124+ try stdout .print (" {s}" , .{subcmd .name });
130125 const padding = 20 - | subcmd .name .len ;
131- for (0.. padding ) | _ | try stdout .writeAll (" " );
132- line = try std .fmt .bufPrint (& buf , "{s}\n " , .{subcmd .description });
133- try stdout .writeAll (line );
126+ for (0.. padding ) | _ | try stdout .writeByte (' ' );
127+ try stdout .print ("{s}\n " , .{subcmd .description });
134128 }
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 );
129+ try stdout .print ("\n " , .{});
130+ try stdout .print ("Run '{s} <COMMAND> --help' for more information on a command.\n\n " , .{command .name });
138131 }
132+
133+ try stdout .flush ();
139134}
0 commit comments