Skip to content

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

@trentm

Description

@trentm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions