Docopt alternatives

60 views
Skip to first unread message

Fraser Scott

unread,
Sep 1, 2015, 4:50:57 PM9/1/15
to pki.io
Hi all,

As part of the refactoring I've kinda gone in a 3 tier CLI direction. The general format is still

pki.io <command> <action> [args]

but I was finding that for different actions overlapping args were clashing. A missing arg for one action should have a default value, but for another action it should be empty (e.g for updates). So, I was trying to do something like:

pki.io help
General help listing all commands

pki.io help command
Command help listing all the actions

pki.io command help action
Action help listing all options

So under this scheme, each action would have it's own docopt usage and therefor individual options, whereas before there was simply one for root and each command (showing all of the actions for that command).

I spent a good few hours on it, but just couldn't get docopt to behave correctly with processing help and showing decent information.... i sort of dislike the minimal usage it sometimes displays.

I had a look around and found https://github.com/spf13/cobra which has exactly the behaviour I was hoping for. It isn't as elegant as docopt, coding the CLI is quite verbose, but it works pretty much perfectly out of the box.

The only downside is that it seems to have quite a few dependencies.

Any thoughts?

Cheers

Fraser Scott

unread,
Sep 2, 2015, 6:50:36 AM9/2/15
to pki.io
The other thing about cobra is that it seems to rely on global variables quite a bit... which feels a bit wrong. Maybe I'm missing something.

Jon Bonazza

unread,
Sep 2, 2015, 10:06:53 AM9/2/15
to Fraser Scott, pki.io

Global variables are 100% okay and even conventional in go code. It's been one of the hardest things for me to get used to in go. Haha


On Wed, Sep 2, 2015, 3:50 AM Fraser Scott <fraser...@gmail.com> wrote:
The other thing about cobra is that it seems to rely on global variables quite a bit... which feels a bit wrong. Maybe I'm missing something.

--
You received this message because you are subscribed to the Google Groups "pki.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pki_io+un...@googlegroups.com.
To post to this group, send email to pki...@googlegroups.com.
Visit this group at http://groups.google.com/group/pki_io.
To view this discussion on the web, visit https://groups.google.com/d/msgid/pki_io/48a8f9a6-f2e1-42dc-aa6f-d24c73c1e365%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fraser Scott

unread,
Sep 2, 2015, 8:16:40 PM9/2/15
to pki.io, fraser...@gmail.com


On Wednesday, 2 September 2015 15:06:53 UTC+1, Jon Bonazza wrote:

Global variables are 100% okay and even conventional in go code. It's been one of the hardest things for me to get used to in go. Haha


Huh. Eww, but huh ;)

Anyway, I *think* I managed to get it working with docopt which is nice because I find it a much more elegant approach and it doesn't  have a ton of dependencies. So far I've only got it right in a little prototype app, but hopefully it will fit into the real app ok.

Athanasios Kostopoulos

unread,
Sep 3, 2015, 2:16:11 AM9/3/15
to Fraser Scott, pki.io
Hi all,
can't we beat docopt into submission (and maybe submit some PRs upstream)? What do you guys think?
Best!

--
You received this message because you are subscribed to the Google Groups "pki.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pki_io+un...@googlegroups.com.
To post to this group, send email to pki...@googlegroups.com.
Visit this group at http://groups.google.com/group/pki_io.

Fraser Scott

unread,
Sep 3, 2015, 4:10:17 AM9/3/15
to Athanasios Kostopoulos, pki.io
On 3 September 2015 at 07:16, Athanasios Kostopoulos <athanasios...@gmail.com> wrote:
Hi all,
can't we beat docopt into submission (and maybe submit some PRs upstream)? What do you guys think?
Best!

Yeah, trying. Not sure how easy it would be to get a PR merged unless it was a Go specific bug. Hopefully it won't come to that. 

Fraser Scott

unread,
Sep 3, 2015, 5:37:42 AM9/3/15
to pki.io, athanasios...@gmail.com
That's it.. I'm ditching the whole "pki.io help <command>" thing. If you want help, either type something wrong or add -h or --help :D

Stupid modern CLIs... get off my lawn!

Fraser Scott

unread,
Sep 4, 2015, 9:41:10 AM9/4/15
to pki.io, athanasios...@gmail.com
And the winner is....


Top level...

$ ./pki.io

Usage: pki.io [OPTIONS] COMMAND [arg...]

Scalable, open source X.509 certificate management

Options:
  -l, --log-level="info"   log level
  --logging=""             alternative logging configuration

Commands:
  init          Initialize an organization
  admin         Manage organization admins
  ca            Manage X.509 Certificate Authorities
  cert          Manage X.509 certificates
  csr           Manage X.509 Certificate Signing Requests
  node          Manage node entities
  org           Manage the organization
  pairing-key   Manage pairing keys

Run 'pki.io COMMAND --help' for more information on a command.


Command ...

$ ./pki.io ca

Usage: pki.io ca COMMAND [arg...]

Manage X.509 Certificate Authorities

Commands:
  new          Create a new CA
  import       Import CA from existing keys
  list         List CAs
  show         Show a CA
  update       Update an existing CA
  delete       Delete a CA

Run 'pki.io ca COMMAND --help' for more information on a command.

Action...

$ ./pki.io ca new
Error: incorrect usage

Usage: pki.io ca new NAME [OPTIONS]

Create a new CA

Arguments:
  NAME=""      name of CA

Options:
  --tags="NAME"      comma separated list of tags
  --ca-expiry=365    CA expiry period in days
  --cert-expiry=90   Certificate expiry period in days
  --key-type="ec"    Key type (ec or rsa)
  --dn-l=""          Locality for DN scope
  --dn-st=""         State/province for DN scope
  --dn-o=""          Organization for DN scope
  --dn-ou=""         Organizational unit for DN scope
  --dn-c=""          Country for DN scope
  --dn-street=""     Street for DN scope
  --dn-postal=""     PostalCode for DN scope


Reply all
Reply to author
Forward
0 new messages