Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write a custom option type to parse -o foo,bar and -o +baz etc. #41

Open
trentm opened this issue Oct 11, 2017 · 1 comment
Open

write a custom option type to parse -o foo,bar and -o +baz etc. #41

trentm opened this issue Oct 11, 2017 · 1 comment

Comments

@trentm
Copy link
Owner

trentm commented Oct 11, 2017

The -H and -o options are ones used commonly (at least in illumos and my tools) for specifying columns for tabular output. One thing I'd like out of those is the ability to have a -o +baz to add the 'baz' field to the default set of columns. Similarly -o -blah to elide just that column.

Here is starter patch I have for sdcadm insts -o ...:

diff --git a/lib/cli/do_instances.js b/lib/cli/do_instances.js
index 27ae7bc..2c2123b 100644
--- a/lib/cli/do_instances.js
+++ b/lib/cli/do_instances.js
@@ -5,9 +5,10 @@
  */
 
 /*
- * Copyright (c) 2015, Joyent, Inc.
+ * Copyright (c) 2017, Joyent, Inc.
  */
 
+var assert = require('assert-plus');
 var tabula = require('tabula');
 
 var common = require('../common');
@@ -17,6 +18,10 @@ var errors = require('../errors');
  * The 'sdcadm instances (insts)' CLI subcommand.
  */
 
+
+var DEFAULT_COLUMNS = 'instance,service,hostname,version,alias';
+
+
 function do_instances(subcmd, opts, args, callback) {
     var self = this;
     if (opts.help) {
@@ -58,8 +63,33 @@ function do_instances(subcmd, opts, args, callback) {
         }
     }
 
+    assert.string(opts.o, 'opts.o');
     /* JSSTYLED */
     var columns = opts.o.trim().split(/\s*,\s*/g);
+    if (columns.length && ['+', '-'].indexOf(columns[0][0]) !== -1) {
+        // If the first char to '-o ARG' is '+' or '-', then this the fields
+        // are "relative" to the default columns.
+        //      `-o -FIELD1,FIELD2` to remove those fields from the default set
+        //      `-o -FIELD1,+FIELD2` to remove FIELD1 and add FIELD2
+        var diffs = columns;
+        /* JSSTYLED */
+        columns = DEFAULT_COLUMNS.split(/\s*,\s*/g);
+        var op; // + or -
+        diffs.forEach(function filterField(diff) {
+            if (diff[0] === '+' || diff[0] === '-') {
+                op = diff[0];
+                diff = diff.slice(1);
+            }
+            if (op === '+') {
+                if (columns.indexOf(diff) === -1) {
+                    columns.push(diff);
+                }
+            } else if (op === '-') {
+                columns.filter(function (field) { return field !== diff; });
+            }
+        });
+    }
+
     /* JSSTYLED */
     var sort = opts.s.trim().split(/\s*,\s*/g);
 
@@ -128,7 +158,7 @@ do_instances.options = [
     {
         names: ['o'],
         type: 'string',
-        default: 'instance,service,hostname,version,alias',
+        default: DEFAULT_COLUMNS,
         help: 'Specify fields (columns) to output.',
         helpArg: 'field1,...'
     },

I think this might nicely fit in a custom option. If so, do that and put it in examples... or possibly even in core dashdash. Then that could be canonical location for this to share in my tools.

@ORESoftware
Copy link
Contributor

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants