forked from harthur/replace
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bnjmnt4n/yargs
Replace `nomnom` with `yargs`.
- Loading branch information
Showing
6 changed files
with
698 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var sharedOptions = require("./shared-options"); | ||
|
||
module.exports = function(scriptName, addlPosArgs, addlOpts) { | ||
addlPosArgs = addlPosArgs || []; | ||
addlOpts = addlOpts || {}; | ||
|
||
var posArgs = {}; | ||
var opts = {}; | ||
Object.keys(sharedOptions).forEach(function(name) { | ||
var option = sharedOptions[name]; | ||
if (typeof option.position === 'number') { | ||
posArgs[name] = option; | ||
} else { | ||
opts[name] = option; | ||
} | ||
}); | ||
|
||
var options = Object.assign({}, opts, addlOpts); | ||
|
||
var positionalArgs = []; | ||
[posArgs, addlPosArgs].forEach(function(posArgs) { | ||
Object.keys(posArgs).forEach(function(name) { | ||
var posArg = posArgs[name]; | ||
posArg.name = name; | ||
positionalArgs[posArg.position] = posArg; | ||
}); | ||
}); | ||
|
||
var command = "$0"; | ||
positionalArgs.forEach(function(positionalArg) { | ||
var option = positionalArg.name; | ||
|
||
if (positionalArg.array) { | ||
option += ".."; | ||
} | ||
if (positionalArg.demandOption) { | ||
option = "<" + option + ">"; | ||
} else { | ||
option = "[" + option + "]"; | ||
} | ||
|
||
command += " " + option; | ||
}); | ||
|
||
return require("yargs") | ||
.scriptName(scriptName) | ||
.command(command, "", function(yargs) { | ||
positionalArgs.forEach(function(positionalArg) { | ||
yargs.positional(positionalArg.name, positionalArg); | ||
}); | ||
}) | ||
.options(options) | ||
.argv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,42 @@ | ||
#!/usr/bin/env node | ||
|
||
var nomnom = require("nomnom"), | ||
replace = require("../replace"), | ||
sharedOptions = require("./shared-options"); | ||
var parseArguments = require("./parse-arguments"), | ||
replace = require("../replace"); | ||
|
||
/* Additional options that apply to `replace`, but not `search` */ | ||
var addlOptions = { | ||
var positionalArgs = { | ||
replacement: { | ||
position: 1, | ||
help: "Replacement string for matches", | ||
type: "string", | ||
required: true | ||
string: true, | ||
describe: "Replacement string for matches", | ||
demandOption: true | ||
}, | ||
paths: { | ||
position: 2, | ||
help: "File or directory to search (default is '*')", | ||
type: "string", | ||
list: true, | ||
array: true, | ||
describe: "File or directory to search", | ||
default: ["*"] | ||
}, | ||
funcFile: { | ||
abbr: 'f', | ||
full: 'function-file', | ||
metavar: 'PATH', | ||
help: 'file containing JS replacement function', | ||
} | ||
}; | ||
|
||
var addlOptions = { | ||
'function-file': { | ||
alias: 'f', | ||
describe: "Path of file containing JS replacement function", | ||
hidden: true | ||
}, | ||
maxLines: { | ||
string: '-n NUMLINES', | ||
help: 'limit the number of lines to preview' | ||
}, | ||
silent: { | ||
abbr: 's', | ||
flag: true, | ||
help: "Don't print out anything" | ||
boolean: true, | ||
describe: "Don't print out anything" | ||
}, | ||
preview: { | ||
abbr: 'p', | ||
flag: true, | ||
help: "Preview the replacements, but don't modify files" | ||
boolean: true, | ||
describe: "Preview the replacements, but don't modify files" | ||
} | ||
} | ||
|
||
var opts = {}; | ||
for (var opt in sharedOptions) { | ||
opts[opt] = sharedOptions[opt]; | ||
} | ||
for (var opt in addlOptions) { | ||
opts[opt] = addlOptions[opt]; | ||
} | ||
|
||
var options = nomnom.options(opts) | ||
.script("replace") | ||
.parse(); | ||
var options = parseArguments("replace", positionalArgs, addlOptions); | ||
|
||
replace(options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
#!/usr/bin/env node | ||
|
||
var nomnom = require("nomnom"), | ||
replace = require("../replace"), | ||
sharedOptions = require("./shared-options"); | ||
var parseArguments = require("./parse-arguments"), | ||
replace = require("../replace"); | ||
|
||
var options = nomnom.options(sharedOptions) | ||
.script("search") | ||
.parse(); | ||
var options = parseArguments("search"); | ||
|
||
replace(options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.