This is all about Slop 3 (ruby 1.9 era). Slop 4 has changed the API.
See the README
And see my new blog post about a Slop 4 example.
One of my favorite features of slop is the automatic help generation.
But it’s not intuitive. It doesn’t print out the help when the parsing
fails. This isn’t very unix-y. So every time I want to use slop, I have to look up this snippet I saved for myself. So I’m posting it here. This is the only slop example
you’ll ever need.
Unix style CLI program in Ruby
Calling it like this will fail:
Great! Then using it correctly will do this:
The resume flag is the only required one, so in this case that’s how
it’s run correctly.
Update: I was very happy that @lee_jarvis (the slop author) accepted my pull request to put
this example into the README.
I wanted to learn about how a 595 timer chip works. I’m a code dude. So when I see this integrated chip and all its pins, it’s scary. I’m sure an EE major is giggling right now but that’s just Impostor Syndrome. For no reason beyond this, I wanted to visualize and grok a 595 timer’s state at any given point by having it’s pins mapped to Redis key/value pairs.
This wasn’t exactly clear. When using the gopkg.in/yaml.v2 (or gopkg.in/yaml.v1) packages, I was confused as to what to do with my data structure.
Let’s say we start off with this file.yml:
Here’s a complete example to read this file in and you get a parsed data structure out of it:
What is Fruits map[string]string in the Config type? It’s foo: ['a', 'b', 'c'].
This is roughly equivalent to what I would do in Ruby. Of course the Ruby code is much shorter because
in Ruby, typically we abuse hashes. :) The surprise I had is this: when the YAML
changes, we have to update our type Config. I’m ok with this. I was just surprised by a few things.
First, the keys are significant. If we change the YAML to be:
It won’t work. But HOW it won’t work is confusing.
You’ll get an empty map until you change your type to have Tambourines in it. You can’t just access
.Tambourines either. The type/struct won’t have a method on it. So this is the trick and benefit the package
gives you. You just model your YAML and it maps the keys for you. But you have to “know” what your
YAML (dare I say schema?) is. So would you then validate that it loaded correctly by checking lengths