blob: 743b1b2292b041023ee53ef89a8d717311b16ba8 [file] [log] [blame]
Seigo Nonaka7e706b02024-08-17 19:16:35 +09001#!/usr/bin/env python
2
3#
4# Copyright (C) 2024 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19"""Build commandline arguments."""
20
21import argparse
22import dataclasses
23from typing import Callable
24
25from alias_builder import Alias
26from alias_builder import parse_aliases_from_json
27from fallback_builder import FallbackEntry
28from fallback_builder import parse_fallback_from_json
29from family_builder import Family
30from family_builder import parse_families_from_json
31
32
33@dataclasses.dataclass
34class CommandlineArgs:
35 outfile: str
36 fallback: [FallbackEntry]
37 aliases: [Alias]
38 families: [Family]
39
40
41def _create_argument_parser() -> argparse.ArgumentParser:
42 """Create argument parser."""
43 parser = argparse.ArgumentParser()
44 parser.add_argument('-o', '--output')
45 parser.add_argument('--alias')
46 parser.add_argument('--fallback')
47 return parser
48
49
50def _fileread(path: str) -> str:
51 with open(path, 'r') as f:
52 return f.read()
53
54
55def parse_commandline(
56 args: [str], fileread: Callable[str, str] = _fileread
57) -> CommandlineArgs:
58 """Parses command line arguments and returns CommandlineArg."""
59 parser = _create_argument_parser()
60 args, inputs = parser.parse_known_args(args)
61
62 families = []
63 for i in inputs:
64 families = families + parse_families_from_json(fileread(i))
65
66 return CommandlineArgs(
67 outfile=args.output,
68 fallback=parse_fallback_from_json(fileread(args.fallback)),
69 aliases=parse_aliases_from_json(fileread(args.alias)),
70 families=families,
71 )