@@ -43,6 +43,12 @@ const cmd = {
43
43
describe : 'Programming language of the source code' ,
44
44
choices : config . sys . langs
45
45
} )
46
+ . option ( 'o' , {
47
+ alias : 'outdir' ,
48
+ type : 'string' ,
49
+ describe : 'Where to save source code' ,
50
+ default : '.'
51
+ } )
46
52
. option ( 'q' , core . filters . query )
47
53
. option ( 't' , core . filters . tag )
48
54
. option ( 'x' , {
@@ -66,12 +72,22 @@ const cmd = {
66
72
}
67
73
} ;
68
74
69
- function genFileName ( problem , lang ) {
75
+ function genFileName ( problem , opts ) {
76
+ const path = require ( 'path' ) ;
77
+ const params = [
78
+ problem . id ,
79
+ problem . slug ,
80
+ '' ,
81
+ h . langToExt ( opts . lang )
82
+ ] ;
83
+
70
84
// try to use a new filename to avoid overwrite by mistake
71
- let name = problem . id + '.' + problem . slug + h . langToExt ( lang ) ;
72
85
let i = 0 ;
73
- while ( fs . existsSync ( name ) )
74
- name = problem . id + '.' + problem . slug + '.' + ( i ++ ) + h . langToExt ( lang ) ;
86
+ let name ;
87
+ do {
88
+ name = path . join ( opts . outdir , params . join ( '.' ) . replace ( / \. + / g, '.' ) ) ;
89
+ params [ 2 ] = i ++ ;
90
+ } while ( fs . existsSync ( name ) ) ;
75
91
return name ;
76
92
}
77
93
@@ -106,7 +122,8 @@ function showProblem(problem, argv) {
106
122
107
123
let filename ;
108
124
if ( argv . gen ) {
109
- filename = genFileName ( problem , argv . lang ) ;
125
+ filename = genFileName ( problem , argv ) ;
126
+ h . mkdir ( argv . outdir ) ;
110
127
fs . writeFileSync ( filename , code ) ;
111
128
112
129
if ( argv . editor !== undefined ) {
0 commit comments