@@ -43,6 +43,12 @@ const cmd = {
4343 describe : 'Programming language of the source code' ,
4444 choices : config . sys . langs
4545 } )
46+ . option ( 'o' , {
47+ alias : 'outdir' ,
48+ type : 'string' ,
49+ describe : 'Where to save source code' ,
50+ default : '.'
51+ } )
4652 . option ( 'q' , core . filters . query )
4753 . option ( 't' , core . filters . tag )
4854 . option ( 'x' , {
@@ -66,12 +72,22 @@ const cmd = {
6672 }
6773} ;
6874
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+
7084 // try to use a new filename to avoid overwrite by mistake
71- let name = problem . id + '.' + problem . slug + h . langToExt ( lang ) ;
7285 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 ) ) ;
7591 return name ;
7692}
7793
@@ -106,7 +122,8 @@ function showProblem(problem, argv) {
106122
107123 let filename ;
108124 if ( argv . gen ) {
109- filename = genFileName ( problem , argv . lang ) ;
125+ filename = genFileName ( problem , argv ) ;
126+ h . mkdir ( argv . outdir ) ;
110127 fs . writeFileSync ( filename , code ) ;
111128
112129 if ( argv . editor !== undefined ) {
0 commit comments