-
Notifications
You must be signed in to change notification settings - Fork 123
CLI for filter #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI for filter #320
Conversation
|
I'm not sure what to do about these failures. I chose to Suggestions welcome. |
kou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interested suggestion. But filter is too generic for command line name to handle only CSV.
How about adding csv- prefix or something?
test/csv/filter/test_filter.rb
Outdated
| # -*- coding: utf-8 -*- | ||
| # frozen_string_literal: false | ||
|
|
||
| require 'minitest/autorun' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're using test-unit not minitest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adapted.
Changed to |
|
Should have added earlier: I've needed substantial debugging code to get this work done, and have left it in (just in case). |
kou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't review all changes. Because there are many parts I need to add comments... Can we start from a smaller script instead of this?
bin/csv-filter
Outdated
|
|
||
| options = {} | ||
|
|
||
| OptionParser.new do |p| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use the following style instead of OptionParser.new do end.parse!? The .parse! part in OptionParser.new do end.parse! is easy to miss.
parser = OptionParser.new
parser.program_name = ...
parser.on(...)
parser.parse!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bin/csv-filter
Outdated
|
|
||
| OptionParser.new do |p| | ||
|
|
||
| p.program_name = File.basename $0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can omit this. We can use the default value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bin/csv-filter
Outdated
|
|
||
| p.program_name = File.basename $0 | ||
| p.version = CSV::VERSION | ||
| p.release = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can omit this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bin/csv-filter
Outdated
| p.program_name = File.basename $0 | ||
| p.version = CSV::VERSION | ||
| p.release = nil | ||
| p.summary_indent = ' ' * 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can omit this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bin/csv-filter
Outdated
| p.version = CSV::VERSION | ||
| p.release = nil | ||
| p.summary_indent = ' ' * 4 | ||
| p.banner = <<-EOF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use BANNER not EOF for here document mark?
EOF (End Of File?) is difficult to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bin/csv-filter
Outdated
|
|
||
| # Returns an array of converter names. | ||
| # \String +s+ contains a comma-separated list of converter names; | ||
| # each optionally begins with a colon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
bin/csv-filter
Outdated
| def parse_converters(s) | ||
| converters = [] | ||
| s.split(',').each do |name| | ||
| name.sub(/^:/, '') | ||
| sym = name.to_sym | ||
| converters.push(sym) | ||
| end | ||
| converters | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def parse_converters(s) | |
| converters = [] | |
| s.split(',').each do |name| | |
| name.sub(/^:/, '') | |
| sym = name.to_sym | |
| converters.push(sym) | |
| end | |
| converters | |
| end | |
| def parse_converters(raw_converters) | |
| raw_converters.split(",").collect(&:to_sym) | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced and renamed.
bin/csv-filter
Outdated
| p.on('--field_size_limit N', | ||
| 'Maximum field size (characters)') do |value| | ||
| options[:field_size_limit] = value.to_i |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| p.on('--field_size_limit N', | |
| 'Maximum field size (characters)') do |value| | |
| options[:field_size_limit] = value.to_i | |
| p.on('--field_size_limit N', Integer, | |
| 'Maximum field size (characters)') do |value| | |
| options[:field_size_limit] = value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
bin/csv-filter
Outdated
| 'Treat the first input row as headers;', | ||
| 'STR is true or comma-separated headers.' | ||
| ) do |value| | ||
| options[:headers] = value == 'true' ? true : parse_converters(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's strange that we use parse_converters for headers.
We need better name than parse_converters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed.
bin/csv-filter
Outdated
| end | ||
| p.on('--liberal_parsing', | ||
| 'Attempt to parse non-conformant input.') do | ||
| options[:liberal_parsing] = :no_argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:no_argument is strange. :liberal_parsing must be true or false: p.on('--[no-]liberal-parsing', ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Symbol :no_argument is only internal, and tells the testing how to behave. The CSV user does not see it.
|
Thanks, @kou, for your interest, and for this helpful review. I'm at RubyConf in Chicago until the weekend, but will work on this when I get home. When we're done with this review cycle, I'll be interested in adding to this work. The filter will not be very useful without a config file (maybe similar to |
|
I have commented out 13 of the options (in Maybe we can work with these, and do the rest piecemeal? |
|
Could you remove them from this PR instead of commenting them out? FYI: You can copy the current branch content: git clone git@github.com:BurdetteLamar/csv.git
cd csv
git switch filter
git switch -c filter-keep
git push origin filter-keepBTW, can remove more options from this PR to make this script smaller? |
I'm thinking to close this PR and start fresh, with just |
|
Yes. It's better than the current PR. |
|
Closing this; will begin again with a new PR. |
Adds a command-line interface for CSV::filter; this supports using CSV without having to write Ruby code.
In its present form, this is a true filter: reads from stdin, writes to stdout. More could be done: input could be from ARGF or an IO stream; output could be to IO stream.