Add a --lang option (en, es, fr) to the greeter #49
Resolved by Add a --lang option (en, es, fr) to the greeter #53, merged by g1tAdd --lang <code> to the hello CLI. Supported: en (default), es, fr.
Expected output:
hello --lang es Ana->¡Hola, Ana!hello --lang fr Luc->Bonjour, Luc !(note the space before!)en/ no flag ->Hi, Ada!as today.
Changes:
src/lib.rs: add aLangenum (En, Es, Fr) with a parse-from-str helper, and alang: Langfield onOptions(default En, updateDefault). Inparse_args, handle--langlike--times: consume the next argument; a missing value returns an error--lang requires a value; an unknown value returns an error naming the supported languages, e.g.--lang value must be one of en, es, fr, got "de". Update theparse_argsdoc comment.- Make greetings language-aware: add e.g.
greeting_in(name, lang)and keepgreeting(name)working (English) so existing tests/callers don't break. Keep the existing trimming andworldfallback for blank names.shoutstill just upper-cases the result. src/main.rs: use the selected language when building the message. Errors already printerror: ...to stderr and exit 2.- Add unit tests in
src/lib.rsand integration tests intests/cli.rs(which has arun(&[..])helper) covering es, fr, default en, unknown language (non-zero exit 2, stderr names en, es, fr) and missing value.
Do not edit README.md or CHANGELOG.md; those are handled in separate issues.
Pull requests
1 for this issue