diff --git a/docs/scripts/p4cpconfig.rst b/docs/scripts/p4cpconfig.rst index 7d60b298..8dc1fa9a 100644 --- a/docs/scripts/p4cpconfig.rst +++ b/docs/scripts/p4cpconfig.rst @@ -66,19 +66,25 @@ General ``--format=FORMAT``, ``-f FORMAT`` Output file format. - Valid formats are ``cmake``, ``env``, and ``json``. + Defaults to ``cmake`` if no value is specified. + ``--load=FILEPATH``, ``-L FILEPATH`` JSON file specifying default configuration settings. - If this parameter is not specified, p4cpconfig will check first - for a file named ``.p4cpconfig`` in the current directory, then for + If no value is specified, p4cpconfig will check first for + a file named ``.p4cpconfig`` in the current directory, then for a file of the same name in the user's home directory. May be suppressed by using an *explicitly unspecified* value (e.g., ``-L-`` or ``--load=None``). +``--output=FILEPATH``, ``-o FILEPATH`` + Output file path. + + Defaults to standard output if no value is specified. + Paths ----- @@ -166,8 +172,8 @@ You can duplicate this behavior by creating a default configuration file: .. code-block:: bash - ./scripts/p4cpconfig.py -L None -f json --target=dpdk \ - -O ovs/install -P install > .p4cpconfig + ./scripts/p4cpconfig.py -L- -f json --target=dpdk \ + -O ovs/install -P install -o .p4cpconfig The file it generates will be something like this: @@ -181,5 +187,5 @@ The file it generates will be something like this: Notes: -* ``-L None`` keeps p4cpconfig from loading the current defaults. +* ``-L-`` keeps p4cpconfig from loading the current defaults. * ``-f json`` tells it to create a json file. diff --git a/scripts/p4cpconfig.py b/scripts/p4cpconfig.py index 35dfdd2e..fb5e7938 100755 --- a/scripts/p4cpconfig.py +++ b/scripts/p4cpconfig.py @@ -332,6 +332,8 @@ def load_defaults(args): def process_args(args): args.variant = None process_build_type_param(args) + process_format_param(args) + process_output_param(args) process_target_param(args) process_path_params(args) return @@ -355,6 +357,25 @@ def process_build_type_param(args): errcount += 1 return +def process_format_param(args): + global errcount + if args.format in UNSPECIFIED: + logging.error("No output format specified") + errcount += 1 + return + format = args.format.lower() + if format in ['cmake', 'env', 'json']: + args.format = format + else: + logging.error("Invalid output format: '%s'", args.format) + errcount += 1 + return + +def process_output_param(args): + if args.outfile in UNSPECIFIED: + args.outfile = '/dev/stdout' + return + def process_target_param(args): global errcount if args.target in UNSPECIFIED: @@ -400,10 +421,12 @@ def parse_args(): parser.add_argument('--format', '-f', type=str, default='cmake', help='output file format (cmake|json|env)') + parser.add_argument('--load', '-L', dest='default_cfg', help='default configuration file') -# parser.add_argument('--output', '-o', -# help='output file path') + + parser.add_argument('--output', '-o', dest='outfile', + help='output file path') # 'paths' group paths = parser.add_argument_group(title='paths') @@ -468,11 +491,13 @@ def str2bool(v): defaults = load_defaults(args) config = define_config(args, defaults) - if args.format == 'cmake': - config.write_cmake_variables(sys.stdout) - elif args.format == 'env': - config.write_env_variables(sys.stdout) - elif args.format == 'json': - config.write_json_params(sys.stdout) + with open(args.outfile, 'w') as outfile: + if args.format == 'cmake': + config.write_cmake_variables(outfile) + elif args.format == 'env': + config.write_env_variables(outfile) + elif args.format == 'json': + config.write_json_params(outfile) + # end with # end __main__