Skip to content

Instantly share code, notes, and snippets.

@sampersand
Last active February 4, 2021 05:58
Show Gist options
  • Save sampersand/65af993a72279821d977ef5404c6db3e to your computer and use it in GitHub Desktop.
Save sampersand/65af993a72279821d977ef5404c6db3e to your computer and use it in GitHub Desktop.
Example of my Inquire Gem
require 'inquire'
# An example of my command-line parsing inquire gem
Inquire.new do
version '2.0a', switch: '-V'
app_name 'cURL2'
author 'Sam'
switch '-v', '--[no-]verbose', 'Enables (or disables) verbose mode', default: false
flag '-H', '--header=HEADER', 'Add a header field', multi: true
flag '-p', '--parameter=DATA', 'Adds a parameter to the uri request', multi: true
flag '-d', '--data=DATA', 'Adds the body for the requst.', required: sub { %w[post put patch].include? @method }
# Example: `curl2 ping localhost:1234`
command 'ping', 'Pings a server' do
positional 'uri', 'The uri to ping.'
end
# Example: `curl2 curl localhost:1234 -XPOST --data=whatever`
command 'curl', 'Sends a normal HTTP request' do
switch '-X', '--method[=METHOD]', 'Sets the method to use', default: 'get', only: %w(get post put delete patch options)
positional 'uri', 'The uri to curl.'
mutualy_exclusive %w[get post put delete patch options custom]
end
# Example: `curl2 rest fetch localhost:1234/api/v1 --id=foo`
command 'rest', 'Sends a REST request' do
json = Flag.new('-j', '--json[=DATA]', 'Indicates data will be json encoded; optionally sets the data too.') do |data|
@json = true
@data = data if data
end
id = Flag.new('--id=VALUE', 'The id to use', required: true)
command 'list', 'List all data.'
command 'fetch', 'Fetch some data.' do
flag id
end
command 'create', 'Create some data.' do
flag json
end
command 'update', 'Update some data.' do
flag json
flag id
end
command 'delete', 'Delete some data.' do
flag id
end
positional 'uri', 'The uri to curl.'
require_one_of %w[list fetch create update delete]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment