• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php根据命令行参数生成配置文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

像npm, composer等工具,在开始使用的使用,都需要初始化项目,生成一个项目的配置文件。这种功能的原理是怎么实现的呢?

比如:

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"

其实很简单,在之前这篇文章php解释命令行的参数的基础上,加上下面的init分支,即可实现类似的功能

#!/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$ 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何利用phpize在生产环境中为php添加新的扩展php-bcmath发布时间:2022-07-10
下一篇:
(基础篇)PHP与Web页面交互发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap