@@ -4624,10 +4624,11 @@ fn cmdTranslateC(
4624
4624
}
4625
4625
4626
4626
const usage_init =
4627
- \\Usage: zig init
4627
+ \\Usage: zig init [path]
4628
4628
\\
4629
- \\ Initializes a `zig build` project in the current working
4630
- \\ directory.
4629
+ \\ Initializes a `zig build` project in the specified
4630
+ \\ path. If path is not provided, initialize in the
4631
+ \\ current working directory.
4631
4632
\\
4632
4633
\\Options:
4633
4634
\\ -s, --strip Generate files without comments
@@ -4640,6 +4641,8 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4640
4641
dev .check (.init_command );
4641
4642
4642
4643
var strip = false ;
4644
+ var path_specified = false ;
4645
+ var path : []const u8 = "" ;
4643
4646
{
4644
4647
var i : usize = 0 ;
4645
4648
while (i < args .len ) : (i += 1 ) {
@@ -4654,7 +4657,12 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4654
4657
fatal ("unrecognized parameter: '{s}'" , .{arg });
4655
4658
}
4656
4659
} else {
4657
- fatal ("unexpected extra parameter: '{s}'" , .{arg });
4660
+ if (path_specified ) {
4661
+ fatal ("more than one path specified" , .{});
4662
+ }
4663
+ path_specified = true ;
4664
+
4665
+ path = arg ;
4658
4666
}
4659
4667
}
4660
4668
}
@@ -4666,6 +4674,19 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4666
4674
const cwd_basename = fs .path .basename (cwd_path );
4667
4675
const sanitized_root_name = try sanitizeExampleName (arena , cwd_basename );
4668
4676
4677
+ var out_dir = fs .cwd ();
4678
+ if (path_specified ) {
4679
+ fs .cwd ().makePath (path ) catch | e |
4680
+ switch (e ) {
4681
+ error .PathAlreadyExists = > return ,
4682
+ error .NotDir = > {
4683
+ fatal ("specified path contains non-directory" , .{});
4684
+ },
4685
+ else = > return e ,
4686
+ };
4687
+ out_dir = try fs .cwd ().openDir (path , .{});
4688
+ }
4689
+
4669
4690
const s = fs .path .sep_str ;
4670
4691
const template_paths = [_ ][]const u8 {
4671
4692
Package .build_zig_basename ,
@@ -4678,7 +4699,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4678
4699
const fingerprint : Package.Fingerprint = .generate (sanitized_root_name );
4679
4700
4680
4701
for (template_paths ) | template_path | {
4681
- if (templates .write (arena , fs . cwd () , sanitized_root_name , template_path , fingerprint )) | _ | {
4702
+ if (templates .write (arena , out_dir , sanitized_root_name , template_path , fingerprint )) | _ | {
4682
4703
std .log .info ("created {s}" , .{template_path });
4683
4704
ok_count += 1 ;
4684
4705
} else | err | switch (err ) {
0 commit comments