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