README.md in anyway_config-1.3.1 vs README.md in anyway_config-1.4.0
- old
+ new
@@ -89,12 +89,12 @@
end
```
#### Customize env variable names prefix
-By default, Anyway Config will use config name with stripped underscores as a prefix for env variable names (e.g.
-`config_name :my_app` will result to parsing `MYAPP_HOST` variable, not `MY_APP_HOST`). You can set env prefix
+By default, Anyway Config uses underscored config name as a prefix for env variable names (e.g.
+`config_name :my_app` will result to parsing `MY_APP_HOST` variable). You can set env prefix
explicitly, and it will be used as is:
```ruby
module MyCoolGem
class Config < Anyway::Config
@@ -103,14 +103,10 @@
attr_config user: 'root', password: 'root', host: 'localhost', options: {}
end
end
```
-**DEPRECATION WARNING** In the 1.4 version no stripping will be applied on config_names by default, so if you use explicit config names with
-underscores and use env variables, your app will be broken. In this case it is recommended to start using `env_prefix`
-now.
-
#### Provide explicit values
Sometimes it's useful to set some parameters explicitly during config initialization.
You can do that using `overrides` option:
@@ -148,29 +144,69 @@
### Using with Ruby
By default, Anyway Config is looking for a config YAML at `./config/<config-name>.yml`. You can override this setting
through special environment variable – 'MYGEM_CONF' – containing the path to the YAML file.
-Environmental variables work the same way as with Rails.
+Environmental variables work the same way as with Rails.
### Config clear and reload
There are `#clear` and `#reload` functions on your config (which do exactly what they state).
Note: `#reload` also accepts `overrides` key to provide explicit values (see above).
+### OptionParser integration
+
+It's possible to use config as option parser (e.g. for CLI apps/libraries). It uses
+[`optparse`](https://ruby-doc.org/stdlib-2.5.1/libdoc/optparse/rdoc/OptionParser.html) under the hood.
+
+Example usage:
+
+```ruby
+class MyConfig < Anyway::Config
+ attr_config :host, :log_level, :concurrency, server_args: {}
+
+ # specify which options shouldn't be handled by option parser
+ ignore_options :server_args
+
+ # provide description for options
+ describe_options(
+ concurrency: "number of threads to use"
+ )
+
+ # extend an option parser object (i.e. add banner or version/help handlers)
+ extend_options do |parser|
+ parser.banner = "mycli [options]"
+ parser.on_tail "-h", "--help" do
+ puts parser
+ end
+ end
+end
+
+config = MyConfig.new
+
+config.parse_options!(%w(--host localhost --port 3333 --log-level debug))
+
+config.host # => "localhost"
+config.port # => 3333
+config.log_level # => "debug"
+
+# Get the instance of OptionParser
+config.option_parser
+```
+
## `Rails.application.config_for` vs `Anyway::Config.for`
-Rails 4.2 introduced new feature: `Rails.application.config_for`. It looks very similar to
+Rails 4.2 introduced new feature: `Rails.application.config_for`. It looks very similar to
`Anyway::Config.for`, but there are some differences:
| Feature | Rails | Anyway Config |
| ------------- |:-------------:| -----:|
| load data from `config/app.yml` | yes | yes |
| load data from `secrets` | no | yes |
| load data from environment | no | yes |
-| return Hash with indifferent access | no | yes |
+| return Hash with indifferent access | no | yes |
| support ERB within `config/app.yml` | yes | yes* |
| raise errors if file doesn't exist | yes | no |
<sub><sup>*</sup>make sure that ERB is loaded</sub>