Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created December 18, 2013 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joyrexus/8025934 to your computer and use it in GitHub Desktop.
Save joyrexus/8025934 to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment