Output in Ruby gem Trollop - ruby

This is an example output from Trollop, the Ruby option parsing gem
v0.0.1a
Options:
--input, -i <s>: Input file location (required)
--output, -o <s>: Output file destination (required)
--cores, -c <i>: Number of cores (default: 4)
--threshold, -t <f>: Threshold (default: 1.0)
--version, -v: Print version and exit
--help, -h: Show this message
It's the best option parser available because it's so small and neat, but I really don't like the centre justification of the help message. I have never seen this kind of output before in programs I have used and would much prefer it to align the options to the left, and then the descriptions to the left in a second column. Is it possible to make it do this?
Cheers
Edit:
In the latest version of trollop this is formatted with a left justification now. It's great! Although I did get used to the centre justification after a short time. Thanks

Nope. Trollop is now at version 2.0.0 and help formatting is still hardcoded in the code. You can hack trollop.rb if you wish to augment to output formatting.

Related

What is the syntax when giving data to an option of a terminal program?

wkhtmltopdf tool convert html document to pdfs. But I want to specify the left margin. What should the syntax be?
$ wkhtmltopdf --margin-left=10 ex1.html ex1.pdf
Unknown long argument --margin-left=10
Every terminal program may vary in its syntax a bit. Try
wkhtmltopdf --help
To see if there's a commands list.
Also found a manual https://wkhtmltopdf.org/usage/wkhtmltopdf.txt when searched for it. Suggests that the syntax is:
wkhtmltopdf --margin-left 10mm
All I needed was to give 1 space between the option and the value and 2 spaces for the next option
wkhtmltopdf --margin-left 10 --margin-right 10 ex1.html ex1.pdf

Convert pcap text file to csv in Bash

The content in the text files have the following formats:
|1=X1|2=Y1|3=K1|4=J1|5=S1|
|1=X2|3=K2|4=J2|5=S2|
|1=X3|2=Y3|4=J3|5=S3|
...
So sometimes it appears there are missing data and what we want is a csv file like the follows:
1,2,3,4,5
X1,Y1,K1,J1,S1
X1,,K2,J2,S2
X3,Y3,,J3,S3
...
I really have no clues on how to do it with a Bash, regarding the missing data.
There are around 5 million lines with 30+ columns and my idea was we might need to do 30x "if clause" to check and fill out ",," for any missing data. This sounds impractical and apparently there should be better methods.
You can either use tshark, or use this program.
pip3 install scapy pyshark
python3 pcap2csv --pcap inp.pcap --csv op.csv
This should work.

Is Rubocop a superset of the "ruby -c" syntax check?

We had a test that found every Ruby file in our application and ran ruby -c on it. We introduced Rubocop and made it check the same list of files.
Is the test that ran ruby -c actually now useless, or is there an example of a failure mode that would be caught by ruby -c but not Rubocop?
The documentation for ruby -c says:
Causes Ruby to check the syntax of the script and exit without executing. If there are no syntax errors, Ruby will print "Syntax OK" to the standard output.
This is an example of a syntax issue that will be caught by either:
% echo "puts 'hello world" > hot_script.rb
% ruby -c hot_script.rb
hot_script.rb:1: unterminated string meets end of file
% rubocop hot_script.rb
Inspecting 1 file
F
Offenses:
hot_script.rb:1:6: F: unterminated string meets end of file
(Using Ruby 1.9 parser; configure using TargetRubyVersion parameter, under AllCops)
puts 'hello world
^
1 file inspected, 1 offense detected
Rubocop even catches some of the same warnings, though I didn't have ruby -c configured to catch these previously, and I am therefore more interested in errors. Here's an example of relative parity in handling a warning:
% cat unused_var.rb
def hot_method
a = 1
b = 2
puts b
end
% ruby -cwW2 unused_var.rb
unused_var.rb:2: warning: assigned but unused variable - a
Syntax OK
% rubocop unused_var.rb
Inspecting 1 file
W
Offenses:
unused_var.rb:2:3: W: Lint/UselessAssignment: Useless assignment to variable - a.
a = 1
^
1 file inspected, 1 offense detected
I searched using
https://www.google.com/search?q=rubocop%20syntax%20check%20superset
https://www.google.com/search?q=is%20there%20any%20reason%20to%20run%20ruby%20-c%20syntax%20check%20if%20i%20use%20rubocop
but I may be doing it wrong. The test is way slower in Ruby 1.9 than it was in Ruby 1.8, so the answer to this question is actually valuable to me. And you have to admit, you're curious, right?
The answer is "most of the time." RuboCop builds on the parser gem, which is a standalone Ruby parser which mimics, more or less, the MRI parser. RuboCop wraps parser's syntax checks and will properly report issues. However, as stated on the parser's GitHub:
Unfortunately MRI often changes syntax in patch level versions
[...] there is no simple way to track these changes.
This policy makes it all but impossible to make Parser precisely
compatible with the Ruby MRI parser.
In addition, parser supports the latest minor version of whatever release you are using, and doesn't backport minor versions. So if you use Ruby 2.4.0, RuboCop will use a parser version supporting 2.4.1 syntax.
For all intents and purposes, parser is equivalent to the official MRI parser, and unless you have a specific reason to use both, using RuboCop alone should be sufficient.
Rubocop will also identify and report syntax errors since the code cannot be properly parsed if that is the case, so there's no need for both.

ruby gem xml-simple: same output for different input

I'm struggling with the xml-simple (1.1.5) gem.
This is my input in test.xml:
<bib>
<title><br/>X</title>
<title>X<br/>X</title>
<title>X<br/></title>
</bib>
Now see what happens using irb as follows:
$ irb -rxmlsimple -rpp
>> pp XmlSimple.xml_in("test.xml")
{"title"=>
[{"br"=>[{}], "content"=>"X"},
{"br"=>[{}], "content"=>["X", "X"]},
{"br"=>[{}], "content"=>"X"}]}
=> {"title"=>[{"br"=>[{}], "content"=>"X"}, {"br"=>[{}], "content"=>["X", "X"]}, {"br"=
>>
So apparently the first and last records, though different, give the same hashes in the output.
Is this a bug?
The xml-simple gem does not work reliably with mixed content. Here's an extract from its documentation:
Mixed content (elements which contain both text content and nested elements) will be not be represented in a useful way - element order and significant whitespace will be lost. If you need to work with mixed content, then XmlSimple is not the right tool for your job.

Colourise ruby syntax from 'cat' output

I want to be able to get colours for Ruby (or C# or F#) in the shells.
wirble does it nicely with IRB, but I want to be able to do:
> cat rakefile.rb | colorize
Does somebody know how I could do this? I know that github's language parsers are OSS - could they be used to read lines one by one and colourise them?
pygments does that for you: http://pygments.org/docs/cmdline/
You can try source-highlight as well.

Resources