Cookie Consent by Free Privacy Policy Generator 📌 CLI in ruby using OptionParser or GetoptLong


✅ CLI in ruby using OptionParser or GetoptLong


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Few days ago I've created a script for the project I'm working on. It was an ugly script with hardcoded values, but it did ther job - create tokens on request. But I've decided to improve it a bit, because sometimes I needed to change params and I've added ability to submit params from the command line. Of course, there are a lot of awesome libraries such as dry-cli, TTY Toolkit or cli-kit from Shopify, but in most cases you can use standard ruby libraries like OptionParser or GetoptLong. Lets see how you can create a CLI utils with those libraries just in few minutes.

We will create simple app, which will ask for your name for the time of the day. If there's no input - script will show you a help screen with list of required and optional arguments.

GetoptLong

We will start with GetoptLong library.

#!/usr/bin/env ruby

require "getoptlong"

opts = GetoptLong.new(
  [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
  [ '--name', '-n', GetoptLong::REQUIRED_ARGUMENT ],
  [ '--time', GetoptLong::OPTIONAL_ARGUMENT ]
)

options = {}

help_message = <<-EOF
Usage: greeting.rb [options]

-h, --help:
   show help

--name yourname, -n yourname:
   your name

--time time_of_the_day:
   time of the day
      EOF

opts.each do |opt, arg|
  case opt

  when "--help"
    puts help_message

  when "--name"
    options[:name] = arg

  when "--time"
    options[:time] = arg
end
end

if options.empty?
  puts help_message
else
  puts "Good #{options[:time]}, #{options[:name]}"
end

Our code is simple - we create an instance of GetoptLong class with list of available params, then in case of match with param name we do execute some code.

OptionParser

Next stop - OptionParser. Similar with GetoptLong we create an instance of OptionParser class with list of available params. There are an opts.banner option which allow us to add some text before list of available params.

#!/usr/bin/env ruby

require 'optparse'


option_parser = OptionParser.new do |opts|
  opts.banner = 'Usage: greeting.rb [options]'

  opts.on '-n', '--name=NAME', 'Set name'
  opts.on '-t', '--time=TIME', 'Set time'
end

options = {}

option_parser.parse!(into: options)

if options.empty?
  puts option_parser
else
  puts "Good #{options[:time]}, #{options[:name]}"
end

Every solution is simple and easy to implement without any additional library, but OptionParser code looks more compact and readable, isn't it?

...

✅ CLI in ruby using OptionParser or GetoptLong


📈 97.86 Punkte

✅ Ruby: Command injection in OptionParser.load


📈 45.01 Punkte

✅ Since xfreerdp is discontinuing its CLI support, what CLI-based xRDP client will you be using?


📈 24.39 Punkte

✅ ruby-grape Gem on Ruby format cross site scripting


📈 20.59 Punkte

✅ private_address_check Ruby Gem up to 0.4.x on Ruby Socket TOCTOU race condition


📈 20.59 Punkte

✅ yajl-ruby gem 1.3.0 on Ruby yajl_encode.c Yajl::Parser.new.parse denial of service


📈 20.59 Punkte

✅ espeak-ruby Gem up to 1.0.2 on Ruby lib/espeak/speech.rb speak/save/bytes/bytes_wav privilege escalation


📈 20.59 Punkte

✅ Ruby After 25 Years by the Creator of Ruby


📈 20.59 Punkte

✅ ruby-grape Gem auf Ruby format Cross Site Scripting


📈 20.59 Punkte

✅ private_address_check Ruby Gem bis 0.4.x auf Ruby Socket TOCTOU Race Condition


📈 20.59 Punkte

✅ Choosing Ruby: What made me choose Ruby as my primary programming language.


📈 20.59 Punkte

✅ yajl-ruby gem 1.3.0 auf Ruby yajl_encode.c Yajl::Parser.new.parse Denial of Service


📈 20.59 Punkte

✅ Semiology in Ruby (What are Ruby Symbols) ?


📈 20.59 Punkte

✅ espeak-ruby Gem bis 1.0.2 auf Ruby lib/espeak/speech.rb speak/save/bytes/bytes_wav erweiterte Rechte


📈 20.59 Punkte

✅ Pixar ruby-jss Gem up to 1.5.x on Ruby XML Document Remote Privilege Escalation


📈 20.59 Punkte

✅ London Trust Media Private Internet Access v82 on Linux /opt/pia/ruby/64/ruby privilege escalation


📈 20.59 Punkte

✅ point-cli Gem 0.0.1 on Ruby Curl Command Line lib/commands/setup.rb information disclosure


📈 20.2 Punkte

✅ point-cli Gem 0.0.1 auf Ruby Curl Command Line lib/commands/setup.rb Information Disclosure


📈 20.2 Punkte

✅ Introducing Eppie-CLI — the official CLI for Eppie, an open protocol encrypted p2p email


📈 19.82 Punkte

✅ The power of the CLI with Golang and Cobra CLI


📈 19.82 Punkte

✅ I created a NordVPN profile manager CLI for OpenVPN called "nordvpn-cli", in bash/node.


📈 19.82 Punkte

✅ Transparently transform the macOS CLI into a fresh GNU/Linux CLI experience.


📈 19.82 Punkte

✅ CVE-2020-3166 | Cisco FXOS CLI Argument input validation (cisco-sa-20200226-fxos-cli-fil)


📈 19.82 Punkte

✅ DD-CLI-HELPER and VBOXMANAGE-CLI-HELPER


📈 19.82 Punkte

✅ O poder do CLI com Golang e Cobra CLI


📈 19.82 Punkte

✅ CVE-2023-20050 | Cisco NX-OS CLI command injection (cisco-sa-nxos-cli-cmdinject-euQVK9u)


📈 19.82 Punkte

✅ halp: A CLI tool to get help with CLI tools


📈 19.82 Punkte

✅ Power Platform CLI Exposed: PAC CLI Overview


📈 19.82 Punkte

✅ CVE-2022-20930 | Cisco SD-WAN CLI argument injection (cisco-sa-sdwan-privesc-cli-xkGwmqKu)


📈 19.82 Punkte

✅ CVE-2022-30301 | Fortinet FortiAP-U CLI up to 5.4.6/6.0.4/6.2.3 CLI Command path traversal


📈 19.82 Punkte

✅ Medium CVE-2020-7633: Apiconnect-cli-plugins project Apiconnect-cli-plugins


📈 19.82 Punkte

✅ The guide to installing and using Ruby on Ubuntu


📈 14.87 Punkte

✅ Using Async in Ruby on Rails for CSV export


📈 14.87 Punkte











matomo

Datei nicht gefunden!