Tools such as npm, composer, etc., need to initialize the project and generate a project configuration file when they are used at the beginning. How does this function work?
for example:
D:\>npm init --yes Wrote to D:\package.json: { "name": "", "version": "1.0.0", "description": "", "main": "index.js", "directories": { "doc": "doc" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"
In fact, it is very simple. On the basis of explaining the parameters of the command line in PHP in the previous article , and adding the following init branch, similar functions can be achieved.
#!/usr/bin/php <?php function init(){ return file_put_contents( getcwd() . '/go.json', '{}' ) . 'bytes has written.' . 'config file has created'; } $res = ''; if( $argc >= 2 ) { $argv[1] == '-v' && $res = 'go version is 1.0'; $argv[1] == 'init' && $res = init(); } echo $res . PHP_EOL;
ghostwu@ghostwu:~/mybin$ ls go2 ghostwu@ghostwu:~/mybin$ go2 init 2bytes has written.config file has created ghostwu@ghostwu:~/mybin$ ls go2 go.json ghostwu@ghostwu:~/mybin$ cat go.json {}ghostwu@ghostwu:~/mybin$