Skip to content

Commit 2e43fc0

Browse files
committed
refs #67: add -o option to show.
Signed-off-by: Eric Wang <[email protected]>
1 parent bf7ce1c commit 2e43fc0

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/commands/show.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -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) {

lib/commands/submission.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const cmd = {
3030
.option('o', {
3131
alias: 'outdir',
3232
type: 'string',
33-
describe: 'Path to save submission code',
33+
describe: 'Where to save submission code',
3434
default: '.'
3535
})
3636
.option('x', {

0 commit comments

Comments
 (0)