0

In my script I use ARGV stream to get command-line arguments. There is a piece of code:

ARGV.each do|a|
item = JSON.parse(%x{curl -X GET https://puppetdb.ai:8081/pdb/query/v4/facts/#{a} --tlsv1 --interface x.x.x.x --cacert certificate.pem --cert certificate-pem.cer --key certificate-privkey.pem})
  item.each do |h|
     arr << {"name" => h['certname'], "#{a}" => h['value'], "environment" => h['environment']}
  end
end

With curl I get some data using puppetdb API. It all works fine, bu I've wanted to add some if statement to print help for script. It looks like that:

ARGV.each do|a|
  if (a == "help")
    puts "this is help 4 U"
  else 
    item = JSON.parse(%x{curl -X GET https://puppetdb.ai:8081/pdb/query/v4/facts/#{a} --tlsv1 --interface x.x.x.x --cacert certificate.pem --cert certificate-pem.cer --key certificate-privkey.pem})
      item.each do |h|
         arr << {"name" => h['certname'], "#{a}" => h['value'], "environment" => h['environment']}
      end
  end
end

but the script ignore "help" and fills in "help" argument to curl. Can you help me with this?

mia102aim
  • 103
  • 1
  • 7
  • 1
    `ruby a.rb help` works for me, printing `"this is help 4 U"` string and immediately exiting. _Sidenote:_ `ARGV` is NOT a stream. – Aleksei Matiushkin Jul 06 '17 at 13:45
  • `ARGV` is an array, `ARGF` is the stream. – Stefan Jul 06 '17 at 13:53
  • Check if you have some whitespaces for some reason. – ndnenkov Jul 06 '17 at 13:55
  • 1
    If you're working with command-line arguments in Ruby then [OptParse](https://ruby-doc.org/stdlib-2.4.1/libdoc/optparse/rdoc/OptionParser.html) is a good place to start. It makes adding `-x` and `--extra` type options easy. – tadman Jul 06 '17 at 16:19
  • Note that instead of using `curl` externally and inhaling the contents via `%x{...}` just `require 'open-uri'`, using the [open-uri library](https://ruby-doc.org/stdlib-2.4.1/libdoc/open-uri/rdoc/OpenURI.html), and `open("https://puppet...") to get the content. – tadman Jul 06 '17 at 16:47

1 Answers1

0

Try using .strip! and look at the Ruby String class documentation.

These can be used to clean up the input as you iterate through each argument supplied with the Ruby command. It may be better to just use the ARGV Array as an Array and look at the Ruby Array class documentation to see the methods methods available to you in Ruby when using the array datatype (which is the form ARGVs come in).. Such as substring search using .include?("help"). Using these method documentation early on is critical to understanding the tools that Ruby provides you.

  • 1
    continually telling the OP to read the docs is not an answer in my opinion, it's a comment – engineersmnky Jul 06 '17 at 17:55
  • I didn't just say read the docs, I explicitly said what in the docs that answer the question, then explain how I got that answer. Because that is important in addition to the answer when learning, its like the teach a person to fish thing. I would never just tell someone to read the docs, thats a waste of both of our time, but I do think citing my sources is worthwhile practice, which is what I feel I did. – broken_historian Jul 06 '17 at 21:03
  • Thanks broken_historian, include? works, but I'll try to use OptParse as tadman suggested, it looks more useful. – mia102aim Jul 07 '17 at 07:57
  • 1
    @broken_historian an answer on a coding forum usually involves code. Most of your answers just seem to suggest where one can find the answer but never really answer the question. (This is not specific to this answer and as shown [Here](https://stackoverflow.com/questions/44954647/how-to-normalize-input-in-mongo-ruby?noredirect=1#comment76908166_44954647) I might not be the only person that feels this way) – engineersmnky Jul 07 '17 at 12:20
  • Since all I have been getting is high reputation people following around all my answers leaving criticism and little more, like most woman I don't feel very welcome here. Ill confine my contributions to open source as code contributions on github and gitlab. @mia102aim: Optsparse is a better way of handling it altogether. – broken_historian Jul 07 '17 at 18:14
  • Your gender is not obvious, or relevant. You are most welcome here. Much of the criticism is relevant. I would urge you to change "was not good enough" to "constantly improving" and have another go at it... – Brad Werth Jul 19 '17 at 00:40