block by joyrexus 8025934

CLI proof-of-concept for explainshell

explainshell.com is a web app for intelligently displaying man page help text for a given command line.

This is helpful and convenient … and anyone working from the command-line is going to want a CLI variant for obvious reasons.

There’s been a number of requests for a CLI on explainshell’s issue list.

In liue of a thin-client that queries a currently non-existing RESTful API service, I suggested replicating the mongodb datastore used to classify the man pages (mongorestore dump/explainshell) and developing a simple CLI for querying the collection.

The following script is intended as a quick proof-of-concept:

client = require('mongodb').MongoClient

url = "mongodb://localhost:27017/explainshell"

query = 
  command: 'cut'
  option:  '-f'

print = (db, err, item) -> 
  options = []
  for p in item.paragraphs
    console.log p.text if p.section is 'SYNOPSIS'
    options.push p.text if p.is_option and p.text.match query.option
  console.log op for op in options
  db.close()

find = (err, db) ->
  return console.dir err if err
  run = (err, item) -> print(db, err, item)
  db.collection('classifier')
    .findOne(name: query.command, run)

client.connect url, find

Running this yields …

cut OPTION... [FILE]...

-f, --fields=LIST
  select  only  these  fields; also print any line that 
  contains no delimiter character, unless the -s option 
  is specified

explain.coffee