Skip to content

Commit 5dc539f

Browse files
authored
feat: added blog post for yargs 5.0.0 release (#29)
1 parent dad7519 commit 5dc539f

1 file changed

Lines changed: 172 additions & 0 deletions

File tree

_posts/2016-08-14-yargs-5.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
layout: blog
3+
title: yargs 5.0.0 Released
4+
excerpt: yargs 5.0.0 released
5+
---
6+
7+
{::options parse_block_html="true" /}
8+
<div class="page-content align-items">
9+
<div class="page-box-full left">
10+
# yargs 5.0.0 Released
11+
12+
_yargs 5.0.0 is currently published to the `next` tag. To try it out simply
13+
type the following:_
14+
15+
<pre><code class="hljs language-bash">npm cache clear; npm i yargs@next --save</code></pre>
16+
17+
This release is an exciting one: we've both introduced several slick new
18+
features (command suggestions, option coercion); and taken the opportunity presented by a major release to cleanup
19+
inconsistencies with yargs' underlying API.
20+
21+
## New Features
22+
23+
### Recommended Commands
24+
25+
I love how git's command-line-interface provides helpful suggestions when
26+
you accidentally type the wrong command:
27+
28+
<pre>
29+
<code class="hljs language-bash">git: 'comit' is not a git command. See 'git --help'.
30+
31+
Did you mean this?
32+
commit</code></pre>
33+
34+
With the new `recommendCommands()` method, you can add this same functionality
35+
to your yargs applications:
36+
37+
<pre>
38+
<code class="hljs language-javascript">require('yargs')
39+
.command('hello', 'hello command')
40+
.command('goodbye', 'goodbye command')
41+
.recommendCommands()
42+
.argv</code></pre>
43+
44+
<pre>
45+
<code class="hljs language-bash">$ ./cli.js helo
46+
47+
Commands:
48+
hello hello command
49+
goodbye goodbye command
50+
51+
Did you mean hello?</code></pre>
52+
53+
### Option Coercion
54+
55+
yargs allows you to provide a `type` parameter for your options, hinting how the value
56+
should be interpreted, e.g., `array`, `boolean`, `string`.
57+
58+
This works great for simple values, but folks routinely ask for more complex
59+
parsing options; as an example, people have asked for the ability to parse
60+
JSON:
61+
62+
<pre><code class="hljs language-bash">$ ./cli.js -j '{"message": "hello world!"}'</code></pre>
63+
64+
yargs' new coercion feature allows you to apply an arbitrary
65+
parsing function to your options:
66+
67+
<pre><code class="hljs language-javascript">var argv = require('./')
68+
.option('j', {
69+
alias: 'json',
70+
coerce: function (arg) {
71+
return JSON.parse(arg)
72+
}
73+
})
74+
.argv
75+
76+
console.log(argv.json.message)</code></pre>
77+
78+
<pre><code class="hljs language-bash">$ ./cli.js '{"message": "hello world!"}'
79+
80+
hello world!</code></pre>
81+
82+
I love the concept of coercion, it gives our users much more flexibility
83+
when parsing their arguments; without adding any significant complexity to
84+
yargs itself.
85+
86+
## Breaking Changes
87+
88+
yargs 5.0.0 introduces several breaking changes, read carefully!
89+
90+
### Fixes to Command API
91+
92+
yargs' [command API](https://github.com/yargs/yargs#commandcmd-desc-builder-handler) is a great way to build a complex nested command
93+
line application; in a nutshell, it allows you to decompose your
94+
command line application into individual commands, helping with the
95+
separation of concerns:
96+
97+
<pre><code class="hljs language-javascript">require('yargs')
98+
.command('foo', 'foo command', function (yargs) {
99+
// builder function, receives a yargs instance
100+
// which you can use to add command-specific options.
101+
}, function (argv) {
102+
// handler function, called once parsing is complete.
103+
console.log('foo')
104+
})
105+
.command('bar', 'bar command', {}, function (argv) {
106+
console.log('bar')
107+
})
108+
.argv</code></pre>
109+
110+
**slick!**
111+
112+
Commands are one of yargs' most complex features, and we continue
113+
to iterate on their API; we've made the following command-related breaking changes
114+
in this release:
115+
116+
* **[default help command added](https://github.com/yargs/yargs/pull/574)**: you can now
117+
simply type `./cli.js help`, to receive yargs' help output.
118+
* **[changes to `demand()`](https://github.com/yargs/yargs/pull/582)**: `demand()` now properly
119+
handles being invoked relative to a command; before this fix, the command itself would cause
120+
constraints to be applied incorrectly.
121+
* **[`builder` no longer requires yargs to be returned](https://github.com/yargs/yargs/pull/549)**: The third
122+
argument to `command` is a builder for describing options specific to the command. The builder command no longer requires that you
123+
return a yargs instance. Simply interact with the yargs instance passed as the first argument, and your options
124+
will take effect.
125+
* **[`builder` now defaults to noop builder](https://github.com/yargs/yargs/pull/583)**: the builder
126+
now resets all yargs configuration settings that are not flagged as being [`global`](https://github.com/yargs/yargs#globalglobals).
127+
* **[global `fail`](https://github.com/yargs/yargs/pull/583)**: the `fail()` method now applies globally. This means that
128+
you can define a `fail()` handler once on the top-level yargs object, and nested failures in commands will bubble up to it.
129+
130+
### Tweaks to Parser
131+
132+
[yargs-parser](https://github.com/yargs/yargs-parser) is the underlying engine that
133+
processes the strings passed into [`process.argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv).
134+
135+
We've made a couple breaking changes to the parser in this release:
136+
137+
* **[we now handle negative numbers](https://github.com/yargs/yargs-parser/pull/44)**: prior to
138+
this release negative numbers were interpreted as numeric options, with this
139+
release you can now pass negative numbers as values to options: `-number -123`.
140+
* **[fixes to option groups](https://github.com/yargs/yargs-parser/pull/19)**: a bug has been
141+
fixed with option groups (`-abc`) terminated by numeric values (`-abc123`).
142+
143+
### Miscellaneous
144+
145+
* **[fixed name of Chinese locale](https://github.com/yargs/yargs/pull/511)**: we've renamed
146+
zh.json to zh_CN.json
147+
148+
## What's Next
149+
150+
The biggest thing currently on the core team's mind is documentation. The [yargs README](https://github.com/yargs/yargs) has grown unwieldy! we have some plans in the
151+
works to help solve this problem:
152+
153+
* over the next few months expect to see targeted tutorials popping up on the yargs
154+
website; the first of which will likely be on the topic of commands.
155+
* we've also created a [gitter.im chatroom](https://gitter.im/yargs/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link) where folks can get help using yargs from its core contributors.
156+
157+
Hope you enjoy all the exciting updates in yargs 5.0.0!
158+
159+
-- [Ben](https://github.com/bcoe).
160+
</div>
161+
162+
{::options parse_block_html="true" /}
163+
<div class="page-box right">
164+
165+
## We got stickers!
166+
167+
[![Yargs Sticker](/images/yargs-laptop-hexagon.png)](https://www.stickermule.com/marketplace/tags/yargs)
168+
169+
Order on [StickerMule](https://www.stickermule.com/marketplace/tags/yargs).
170+
171+
</div>
172+
</div>

0 commit comments

Comments
 (0)