hurl/bin/spec/options/generate_source.py

97 lines
2.9 KiB
Python
Raw Normal View History

2023-11-02 23:32:03 +03:00
#!/usr/bin/env python3
import sys
from option import Option
import sys
"""
Generate source file for clap
"""
COPYRIGHT = """/*
* Hurl (https://hurl.dev)
2024-02-24 20:29:01 +03:00
* Copyright (C) 2024 Orange
2023-11-02 23:32:03 +03:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/"""
import os
2023-11-02 23:32:03 +03:00
import sys
# Relative script file
SCRIPT_FILE = __file__[len(os.getcwd()) + 1 :]
2023-11-02 23:32:03 +03:00
2024-03-15 18:45:52 +03:00
def generate_source(options: list[Option]) -> str:
2023-11-02 23:32:03 +03:00
s = COPYRIGHT
s += "\n" + "// Generated by " + SCRIPT_FILE + " - Do not modify"
2024-01-18 16:33:56 +03:00
# s += "\nuse clap::{value_parser, ArgAction};"
2023-11-02 23:32:03 +03:00
s += """\n\npub fn input_files() -> clap::Arg {
clap::Arg::new("input_files")
.value_name("FILES")
.help("Set the input file to use")
.required(false)
.index(1)
.num_args(1..)
}"""
for option in options:
s += "\n\n" + generate_source_option(option)
return s
2024-03-15 18:45:52 +03:00
def generate_source_option(option: Option) -> str:
2023-11-02 23:32:03 +03:00
s = f"pub fn {option.name}() -> clap::Arg {{"
s += f'\n clap::Arg::new("{option.name}")'
s += f'\n .long("{option.long}")'
if option.short is not None:
s += f"\n .short('{option.short}')"
if option.value is not None:
s += f'\n .value_name("{option.value}")'
if option.value_default is not None:
s += f'\n .default_value("{option.value_default}")'
if option.value_parser is not None:
s += f"\n .value_parser({option.value_parser})"
if "-1" in option.value_parser:
s += f"\n .allow_hyphen_values(true)"
s += f'\n .help("{option.help}")'
if option.conflict is not None:
2024-01-18 16:33:56 +03:00
for conflict in option.conflict:
s += f'\n .conflicts_with("{conflict}")'
2023-11-02 23:32:03 +03:00
if option.value is not None:
s += f"\n .num_args(1)"
else:
2024-01-18 16:33:56 +03:00
s += f"\n .action(clap::ArgAction::SetTrue)"
2023-11-02 23:32:03 +03:00
if option.append:
2024-01-18 16:33:56 +03:00
s += f"\n .action(clap::ArgAction::Append)"
2024-03-15 18:45:52 +03:00
if option.deprecated or option.experimental:
2023-11-02 23:32:03 +03:00
s += f"\n .hide(true)"
s += "\n}"
return s
def main():
# Parse all options file given at the command line
if len(sys.argv) < 2:
print("usage: generate_source.py OPTION_FILE1 OPTION_FILE2 ...")
sys.exit(1)
options = sorted(
[Option.parse_file(filename) for filename in sys.argv[1:]],
key=lambda option: option.name,
)
print(generate_source(options))
if __name__ == "__main__":
main()