• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 
25 #include "common/libs/utils/result.h"
26 #include "host/commands/cvd/client.h"
27 #include "host/commands/cvd/flag.h"
28 #include "host/commands/cvd/selector/arguments_separator.h"
29 #include "host/commands/cvd/types.h"
30 
31 namespace cuttlefish {
32 
33 /* the very first command line parser
34  *
35  * Being aware of valid subcommands and cvd-specific commands, it will
36  * separate the command line arguments into:
37  *
38  *  1. program path/name
39  *  2. cvd-specific arguments
40  *     a) selector flags
41  *     b) non-selector flags
42  *  3. subcommand
43  *  4. subcommand arguments
44  *
45  * This is currently on the client side but will be moved to the server
46  * side.
47  */
48 class FrontlineParser {
49   using ArgumentsSeparator = selector::ArgumentsSeparator;
50 
51  public:
52   struct ParserParam {
53     // commands supported by the server
54     std::vector<std::string> server_supported_subcmds;
55     // commands supported by the client itself
56     std::vector<std::string> internal_cmds;
57     cvd_common::Args all_args;
58     FlagCollection cvd_flags;
59   };
60 
61   // This call must guarantee all public methods will be valid
62   static Result<std::unique_ptr<FrontlineParser>> Parse(ParserParam param);
63 
64   const std::string& ProgPath() const;
65   std::optional<std::string> SubCmd() const;
66   const cvd_common::Args& SubCmdArgs() const;
67   const cvd_common::Args& CvdArgs() const;
68 
69  private:
70   FrontlineParser(const ParserParam& parser);
71 
72   // internal workers in order
73   Result<void> Separate();
74   Result<cvd_common::Args> ValidSubcmdsList();
75   Result<std::unique_ptr<ArgumentsSeparator>> CallSeparator();
76   struct FilterOutput {
77     bool clean;
78     bool help;
79     cvd_common::Args selector_args;
80   };
81   Result<FilterOutput> FilterNonSelectorArgs();
82 
83   cvd_common::Args server_supported_subcmds_;
84   const cvd_common::Args all_args_;
85   const std::vector<std::string> internal_cmds_;
86   FlagCollection cvd_flags_;
87   std::unique_ptr<ArgumentsSeparator> arguments_separator_;
88 };
89 
90 }  // namespace cuttlefish
91