在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:renatosilva/easyoptions开源软件地址:https://github.com/renatosilva/easyoptions开源编程语言:Ruby 53.9%开源软件介绍:EasyOptionsEasyOptions allows you to write the help text for your program only once, and have the described options automatically parsed from command line into easily readable variables, without complicated API. EasyOptions was developed after discontentment with the existing solutions for option parsing in Bash and Ruby. It was conceived with the following guidelines in mind:
EasyOptions is going to parse all of your options and arguments automatically once sourced. You specify what options are supported by your program by simply writing a help text, using special double-hash comments. This help text also works at the same time as source code documentation and options specification. All client scripts have an automatic UsageFor using EasyOptions in your script, simply document it using double-hash comments like this:
The above comments work both as source code documentation and as help text, as well as define the options supported by your script. There is no duplication of the options specification. The string For Ruby
After writing your documentation, you simply require this script. Then all command line options will get parsed into the require "easyoptions"
options, arguments = EasyOptions.all
# Boolean options
puts "Option specified: --some-option" if options[:some_option]
puts "Option specified: --some-boolean" if options[:some_boolean]
# Parameter option
value = options[:some_value]
if value
type = value.is_a?(Fixnum)? "number" : "string"
puts "Option specified: --some-value is #{value} (a #{type})"
end
# Arguments
exit if arguments.empty?
arguments.each do |argument|
puts "Argument specified: #{argument}"
end For BashAfter writing your documentation, you simply source this script. Then all command line options will get parsed into the corresponding variables. You can then check their values for reacting to them. Regular arguments will be available in the source easyoptions || exit
# Boolean and parameter options
[[ -n "$some_option" ]] && echo "Option specified: --some-option"
[[ -n "$some_boolean" ]] && echo "Option specified: --some-boolean"
[[ -n "$some_value" ]] && echo "Option specified: --some-value is $some_value"
# Arguments
for argument in "${arguments[@]}"; do
echo "Argument specified: $argument"
done If using the pure Bash implementation, then for better speed you may want to define the options in source code yourself, so they do not need to be parsed from the documentation. The side effect is that when changing them, you will need to update both the documentation and the source code. You define the options statically like this: options=(o=option some-boolean some-value=?) ContributingThe principle behind EasyOptions can be applied in other scripting languages and possibly static ones. If you would like to contribute, below are some enhancements that would be welcome. You can also check the list of issues and contribute a patch.
License and copyrightCopyright (c) 2013, 2014 Renato Silva. Licensed under the terms of the 3-clause BSD. |
2022-08-15
2022-08-17
2023-10-27
2022-09-23
2022-08-18
请发表评论