Default Value support
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Ruby
- Estrellas
- 71
- Forks
- 27
- Merge medio
- 3 h 3 min
- PR fusionados (30 d)
- 2
Descripción
Hi,
Thanks a lot for this gem!
I wrote a little hacky class that adds support for default values. So you can do:
parser.on('-m', '--tracing-mode MODE', 'Define the category of events traced', default: 'default')
I have no idea if you can find it interesting, but I use it quite often in my project, so I share it with you. Putting the default values into the into for me to duplicate the key (one into the the on and one into the into. I lead from some silly mistake due to a typo).
If you are interested, maybe I can try to do a cleaner PR. If you are not, please feel free to close this issue.
Thanks a lot!
require 'optparse'
class OptionParserWithDefaultAndValidation < OptionParser
def initialize
# Dictionary to save the default values passed by `on`
@defaults = {}
super
end
def parse!(argv = default_argv, into: nil)
# Merge the default value with the `into` dict
into.merge!(@defaults) unless into.nil?
# This will populate the `into` dict (overwriting our default if needed)
super
end
def define(*opts, &block)
switch = super
@defaults[switch.switch_name] = @tmp_default unless @tmp_default.nil?
switch
end
def on(*opts, default, &block)
# Save the default, which will be used in `refine.`
@tmp_default = default
# Default can contain Array (or Set).
# We cannot use the [a].flatten trick.
# So we use the respond_to one
# irb(main):028:0> default = Set[-1,2]
# => #<Set: {-1, 2}>
# irb(main):029:0> [default].flatten.join(',')
# => "#<Set: {-1, 2}>"
# irb(main):030:0> default.respond_to?(:flatten)? default.flatten.join(',') : default
# => "-1,2"
opts << "Default: #{default.respond_to?(:flatten) ? default.flatten.join(',') : default}" unless default.nil?
super(*opts, &block)
end
end
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con los puntos de entrada OptionParser#on, #define y #parse! mostrados en la subclase propuesta, y revisa cómo se rellena el argumento existente into. Confirma el comportamiento esperado para los valores predeterminados con y sin into; después, localiza las pruebas existentes de OptionParser y úsalas para definir los criterios de finalización.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- ruby
- Área
- cli
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 35/100