Skip to content

Commit 9b4eb95

Browse files
committed
Clarify args parsing, update docs
- Update yargs config - Add examples etc. to --help message - Update README.md
1 parent bc2b528 commit 9b4eb95

2 files changed

Lines changed: 122 additions & 63 deletions

File tree

README.md

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
[![npm][npm]][npm-url]
2-
[![node][node]][node-url]
3-
[![deps][deps]][deps-url]
41
[![tests][tests]][tests-url]
2+
[![cover][cover]][cover-url]
3+
[![deps][deps]][deps-url]
4+
[![npm][npm]][npm-url]
55
[![code style][style]][style-url]
66
[![chat][chat]][chat-url]
77

@@ -13,68 +13,72 @@
1313
<h1>PostCSS CLI</h1>
1414
</div>
1515

16-
## Install
16+
## Installation
1717

1818
```bash
1919
npm i -D postcss-cli
2020
```
2121

2222
## Usage
2323

24-
```bash
25-
postcss [options] [input] [-o output|-d output-dir|-r] [--watch]
24+
```
25+
postcss [input.css] [OPTIONS] [--output|-o output.css] [--watch]
26+
27+
Options:
28+
-o, --output Output file [string]
29+
-d, --dir Output directory [string]
30+
-r, --replace Replace (overwrite) the input file [boolean]
31+
-u, --use List of postcss plugins to use [array]
32+
-p, --parser Custom postcss parser [string]
33+
-t, --stringifier Custom postcss stringifier [string]
34+
-s, --syntax Custom postcss syntax [string]
35+
-w, --watch Watch files for changes and recompile as needed [boolean]
36+
-x, --extension Override the output file extension [string]
37+
-e, --env A shortcut for setting NODE_ENV [string]
38+
-c, --config Set a custom path to look for a config file [string]
39+
-m, --map Create an external sourcemap
40+
--no-map Disable the default inline sourcemaps
41+
-h, --help Show help [boolean]
42+
-v, --version Show version number [boolean]
43+
44+
Examples:
45+
postcss input.css -o output.css Basic usage
46+
cat input.css | postcss -u Piping input & output
47+
autoprefixer > output.css
48+
49+
If no input files are passed, it reads from stdin. If neither -o, --dir, or
50+
--replace is passed, it writes to stdout.
51+
52+
If there are multiple input files, the --dir or --replace option must be passed.
2653
```
2754

28-
## Options
29-
30-
|Name|Default|Description|
31-
|:--:|:-----:|:----------|
32-
|**`--dir/-d`**|`undefined`|Directory destination|
33-
|**`--ext/-ex`**|`undefined`|Change Output File Extension|
34-
|**`--output/-o`**|`process.stdout`|File destination|
35-
|**`--replace/-r`**|`undefined`|Replace file(s) with output|
36-
|**`--parser/-p`**|`undefined`|[Custom Parser][parser] (e.g SugarSS)|
37-
|**`--syntax/-s`**|`undefined`|[Custom Syntax][syntax] (e.g Midas)|
38-
|**`--stringifier/-t`**|`undefined`|[Custom Stringifier][stringifier] (e.g ..)|
39-
|**`--map/-m`**|`{ inline: false }`|Enable external Sourcemaps|
40-
|**`--no-map`**|`false`|Disable Sourcemaps|
41-
|**`--watch/-w`**|`false`|Watch files && `postcss.config.js`for changes|
42-
|**`--help/-h`**|`undefined`|CLI Usage|
43-
|**`--version/-v`**|`undefined`|CLI Version|
44-
45-
[parser]: https://github.com/postcss/postcss#custom-syntaxes
46-
47-
#### `--map|-m`
55+
For more details on custom parsers, stringifiers and syntaxes, see the [postcss docs](https://github.com/postcss/postcss#syntaxes).
4856

49-
Activate sourcemaps generation. By default inline sourcemaps are generated.
50-
You can use [advances source map options][sourcemaps].
57+
## Configuration
5158

52-
#### [`--config|-c`](https://github.com/michael-ciniawsky/postcss-load-config)
59+
If you need to pass options to your plugins, or have a long plugin chain, you'll want to use a configuration file.
5360

54-
```bash
55-
postcss -c|--config path/to/postcss.config.js
56-
```
57-
**postcss.config.js**
61+
Example `postcss.config.js`:
5862

5963
```js
60-
module.exports = (ctx) => ({
61-
parser: ctx.options.parser ? 'sugarss' : false,
62-
map: ctx.env === 'development' ? 'inline' : false,
63-
plugins: {
64-
'postcss-import': { root: ctx.file.dirname },
65-
'postcss-nested': {},
66-
'cssnano': ctx.env !== 'development' ? {} : false
67-
}
68-
})
64+
module.exports = () => {
65+
parser: 'sugarss',
66+
plugins: [
67+
require('postcss-import')({ /* Options */ }),
68+
require('postcss-url')({
69+
url: 'copy',
70+
useHash: true
71+
})
72+
]
73+
}
6974
```
7075

76+
Configuration files are handled by [postcss-load-config](https://github.com/michael-ciniawsky/postcss-load-config). Refer to the docs there for more details.
77+
7178

7279
[npm]: https://img.shields.io/npm/v/postcss-cli.svg
7380
[npm-url]: https://npmjs.com/package/postcss-cli
7481

75-
[node]: https://img.shields.io/node/v/<name>-loader.svg
76-
[node-url]: https://nodejs.org
77-
7882
[deps]: https://img.shields.io/gemnasium/postcss/postcss-cli.svg
7983
[deps-url]: https://gemnasium.com/postcss/postcss-cli
8084

index.js

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,81 @@ const version = () => {
4242
}
4343

4444
const argv = require('yargs')
45-
.usage(`${chalk.bold.red(logo)}\nUsage: \n\n$0 [--config|-c path/to/postcss.config.js] [input.css] [--output|-o output.css] [-watch|-w]`)
46-
.alias('e', 'env').describe('e', 'Environment')
47-
.alias('x', 'ext').describe('x', 'Extension')
48-
.alias('c', 'config').describe('c', 'Config')
49-
.alias('o', 'output').describe('o', 'Output')
50-
.describe('concat', 'Concat Input')
51-
.alias('d', 'dir').describe('d', 'Output Directory')
52-
.alias('r', 'replace').describe('r', 'Replace File')
45+
.usage(
46+
`${chalk.bold.red(logo)}
47+
Usage:
48+
49+
$0 [input.css] [OPTIONS] [--output|-o output.css] [--watch]`
50+
)
51+
.option('o', {
52+
alias: 'output',
53+
desc: 'Output file',
54+
type: 'string'
55+
})
56+
.option('d', {
57+
alias: 'dir',
58+
desc: 'Output directory',
59+
type: 'string'
60+
})
61+
.option('r', {
62+
alias: 'replace',
63+
desc: 'Replace (overwrite) the input file',
64+
type: 'boolean'
65+
})
66+
.option('u', {
67+
alias: 'use',
68+
desc: 'List of postcss plugins to use',
69+
type: 'array'
70+
})
71+
.option('p', {
72+
alias: 'parser',
73+
desc: 'Custom postcss parser',
74+
type: 'string'
75+
})
76+
.option('t', {
77+
alias: 'stringifier',
78+
desc: 'Custom postcss stringifier',
79+
type: 'string'
80+
})
81+
.option('s', {
82+
alias: 'syntax',
83+
desc: 'Custom postcss syntax',
84+
type: 'string'
85+
})
86+
.option('w', {
87+
alias: 'watch',
88+
desc: 'Watch files for changes and recompile as needed',
89+
type: 'boolean'
90+
})
91+
.option('x', {
92+
alias: 'ext',
93+
desc: 'Override the output file extension',
94+
type: 'string'
95+
})
96+
.option('e', {
97+
alias: 'env',
98+
desc: 'A shortcut for setting NODE_ENV',
99+
type: 'string'
100+
})
101+
.option('c', {
102+
alias: 'config',
103+
desc: 'Set a custom path to look for a config file',
104+
type: 'string'
105+
})
53106
.alias('m', 'map')
54-
.describe('m', 'External Sourcemap')
55-
.describe('no-map', 'Disable Sourcemaps')
56-
.alias('b', 'concat').describe('b', 'Concat Input')
57-
.alias('u', 'use').describe('u', 'Plugin(s)').array('u')
58-
.alias('p', 'parser').describe('p', 'Parser')
59-
.alias('s', 'syntax').describe('s', 'Syntax')
60-
.alias('t', 'stringifier').describe('t', 'Stringifier')
61-
.alias('w', 'watch').describe('w', 'Watch')
62-
.help('h').alias('h', 'help')
107+
.describe('m', 'Create an external sourcemap')
108+
.describe('no-map', 'Disable the default inline sourcemaps')
63109
.version(version).alias('v', 'version')
64-
.requiresArg(['i', 'o'])
110+
.help('h').alias('h', 'help')
111+
.example('$0 input.css -o output.css', 'Basic usage')
112+
.example('cat input.css | $0 -u autoprefixer > output.css', 'Piping input & output')
113+
.epilog(
114+
`If no input files are passed, it reads from stdin. If neither -o, --dir, or --replace is passed, it writes to stdout.
115+
116+
If there are multiple input files, the --dir or --replace option must be passed.
117+
118+
For more details, please see https://github.com/postcss/postcss-cli`
119+
)
65120
.argv
66121

67122
let dir = argv.dir

0 commit comments

Comments
 (0)