resolves #132 fix options object (#171)

* resolves #132 fix options object

* Add Documentation for --details parameter

* fix spacing issue

* update naming of options parameter

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2018-02-17 04:33:39 +05:30
committed by Jeffrey Warren
parent bbef4bca57
commit fd45d3caf3
2 changed files with 101 additions and 56 deletions

View File

@@ -98,7 +98,7 @@ Image Sequencer also provides a CLI for applying operations to local files. The
-s | --step [step-name] | Name of the step to be added. (required)
-b | --basic | Basic mode only outputs the final image
-o | --output [PATH] | Directory where output will be stored. (optional)
-op | --opions {object} | Options for the step. (optional)
-d | --details {object} | Options for the step. (optional)
The basic format for using the CLI is as follows:
@@ -116,6 +116,15 @@ The CLI also can take multiple steps at once, like so:
But for this, double quotes must wrap the space-separated steps.
Options for the steps can be passed in one line as json in the details option like
```
$ ./index.js -i [PATH] -s "brightness" -d '{"brightness":50}'
```
Or the values can be given through terminal prompt like
<img width="1436" alt="screen shot 2018-02-14 at 5 18 50 pm" src="https://user-images.githubusercontent.com/25617855/36202790-3c6e8204-11ab-11e8-9e17-7f3387ab0158.png">
## Classic Usage
### Initializing the Sequencer

View File

@@ -12,13 +12,13 @@ function exit(message) {
}
program
.version('0.1.0')
.option('-i, --image [PATH/URL]', 'Input image URL')
.option('-s, --step [step-name]', 'Name of the step to be added.')
.option('-o, --output [PATH]', 'Directory where output will be stored.')
.option('-b, --basic','Basic mode outputs only final image')
.option('-op, --opions {object}', 'Options for the step')
.parse(process.argv);
.version('0.1.0')
.option('-i, --image [PATH/URL]', 'Input image URL')
.option('-s, --step [step-name]', 'Name of the step to be added.')
.option('-o, --output [PATH]', 'Directory where output will be stored.')
.option('-b, --basic','Basic mode outputs only final image')
.option('-c, --config [Object]', 'Options for the step')
.parse(process.argv);
// Parse step into an array to allow for multiple steps.
if(!program.step) exit("No steps passed")
@@ -34,7 +34,7 @@ require('fs').access(program.image, function(err){
// User must input a step. If steps exist, check that every step is a valid step.
if(!program.step || !validateSteps(program.step))
exit("Please ensure all steps are valid.");
exit("Please ensure all steps are valid.");
// If there's no user defined output directory, select a default directory.
program.output = program.output || "./output/";
@@ -81,12 +81,30 @@ sequencer.loadImages(program.image,function(){
console.log(new Array(step.length + 5).join(' ') + input + ": " + options[input].desc);
});
if(program.config){
try{
program.config = JSON.parse(program.config);
console.log(`The parsed options object: `, program.config);
}
catch(e){
console.error('\x1b[31m%s\x1b[0m',`Options(Config) is not a not valid JSON Fallback activate`);
program.config = false;
console.log(e);
}
}
if(program.config && validateConfig(program.config,options)){
console.log("Now using Options object");
Object.keys(options).forEach(function (input) {
options[input] = program.config[input];
})
}
else{
// If inputs exist, iterate through them and prompt for values.
Object.keys(options).forEach(function(input) {
var value = readlineSync.question("[" + step + "]: Enter a value for " + input + " (" + options[input].type + ", default: " + options[input].default + "): ");
options[input] = value;
});
}
// Add the step and its inputs to the sequencer.
sequencer.addSteps(step, options);
});
@@ -105,7 +123,7 @@ sequencer.loadImages(program.image,function(){
});
// Takes an array of steps and checks if they are valid steps for the sequencer.
function validateSteps(steps) {
function validateSteps(steps) {
// Assume all are valid in the beginning.
var valid = true;
@@ -119,3 +137,21 @@ sequencer.loadImages(program.image,function(){
// Return valid. (If all of the steps are valid properties, valid will have remained true).
return valid;
}
//Takes config and options object and checks if all the keys exist in config
function validateConfig(config_,options_){
options_ = Object.keys(options_);
if (
(function(){
for(var input in options_){
if(!config_[options_[input]]){
console.error('\x1b[31m%s\x1b[0m',`Options Object does not have the required details "${options_[input]}" not specified. Fallback case activated`);
return false;
}
}
})()
== false)
return false;
else
return true;
}