@@ -4612,10 +4612,11 @@ fn cmdTranslateC(
4612
4612
}
4613
4613
4614
4614
const usage_init =
4615
- \\Usage: zig init
4615
+ \\Usage: zig init [path]
4616
4616
\\
4617
- \\ Initializes a `zig build` project in the current working
4618
- \\ directory.
4617
+ \\ Initializes a `zig build` project in the specified
4618
+ \\ path. If path is not provided, initialize in the
4619
+ \\ current working directory.
4619
4620
\\
4620
4621
\\Options:
4621
4622
\\ -s, --strip Generate files without comments
@@ -4628,6 +4629,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4628
4629
dev .check (.init_command );
4629
4630
4630
4631
var strip = false ;
4632
+ var path : ? []const u8 = "" ;
4631
4633
{
4632
4634
var i : usize = 0 ;
4633
4635
while (i < args .len ) : (i += 1 ) {
@@ -4642,7 +4644,10 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4642
4644
fatal ("unrecognized parameter: '{s}'" , .{arg });
4643
4645
}
4644
4646
} else {
4645
- fatal ("unexpected extra parameter: '{s}'" , .{arg });
4647
+ if (path != null ) {
4648
+ fatal ("more than one path specified" , .{});
4649
+ }
4650
+ path = arg ;
4646
4651
}
4647
4652
}
4648
4653
}
@@ -4654,6 +4659,19 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4654
4659
const cwd_basename = fs .path .basename (cwd_path );
4655
4660
const sanitized_root_name = try sanitizeExampleName (arena , cwd_basename );
4656
4661
4662
+ var out_dir = fs .cwd ();
4663
+ if (path != null ) {
4664
+ fs .cwd ().makePath (path .? ) catch | e |
4665
+ switch (e ) {
4666
+ error .PathAlreadyExists = > return ,
4667
+ error .NotDir = > {
4668
+ fatal ("specified path contains non-directory" , .{});
4669
+ },
4670
+ else = > return e ,
4671
+ };
4672
+ out_dir = try fs .cwd ().openDir (path .? , .{});
4673
+ }
4674
+
4657
4675
const s = fs .path .sep_str ;
4658
4676
const template_paths = [_ ][]const u8 {
4659
4677
Package .build_zig_basename ,
@@ -4666,7 +4684,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4666
4684
const fingerprint : Package.Fingerprint = .generate (sanitized_root_name );
4667
4685
4668
4686
for (template_paths ) | template_path | {
4669
- if (templates .write (arena , fs . cwd () , sanitized_root_name , template_path , fingerprint )) | _ | {
4687
+ if (templates .write (arena , out_dir , sanitized_root_name , template_path , fingerprint )) | _ | {
4670
4688
std .log .info ("created {s}" , .{template_path });
4671
4689
ok_count += 1 ;
4672
4690
} else | err | switch (err ) {
0 commit comments