master
h 108 lines 2.32 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 Invocation.h
8
9 Abstract:
10
11 Header file for walking through and processing a command line invocation.
12
13 --*/
14 #pragma once
15 #include <string>
16 #include <vector>
17
18 namespace wsl::windows::wslc {
19 struct Invocation
20 {
21 Invocation(std::vector<std::wstring>&& args) : m_args(std::move(args))
22 {
23 }
24
25 struct iterator
26 {
27 iterator(size_t arg, std::vector<std::wstring>& args) : m_arg(arg), m_args(args)
28 {
29 }
30
31 iterator(const iterator&) = default;
32 iterator& operator=(const iterator&) = default;
33
34 iterator operator++()
35 {
36 return {++m_arg, m_args};
37 }
38 iterator operator++(int)
39 {
40 return {m_arg++, m_args};
41 }
42 iterator operator--()
43 {
44 return {--m_arg, m_args};
45 }
46 iterator operator--(int)
47 {
48 return {m_arg--, m_args};
49 }
50
51 bool operator==(const iterator& other) const
52 {
53 return m_arg == other.m_arg;
54 }
55 bool operator!=(const iterator& other) const
56 {
57 return m_arg != other.m_arg;
58 }
59
60 const std::wstring& operator*() const
61 {
62 return m_args[m_arg];
63 }
64 const std::wstring* operator->() const
65 {
66 return &(m_args[m_arg]);
67 }
68
69 size_t index() const
70 {
71 return m_arg;
72 }
73
74 private:
75 size_t m_arg;
76 std::vector<std::wstring>& m_args;
77 };
78
79 size_t size() const
80 {
81 return m_args.size();
82 }
83 iterator begin()
84 {
85 return {m_currentFirstArg, m_args};
86 }
87 iterator end()
88 {
89 return {m_args.size(), m_args};
90 }
91 // Marks i as consumed: the next begin() returns i + 1.
92 void consume(const iterator& i)
93 {
94 m_currentFirstArg = i.index() + 1;
95 }
96 // Sets the start of the unconsumed range to i: the next begin() returns i.
97 // Use this when a parser stopped at an unconsumed token (e.g. options-only
98 // parsing that stopped on the first positional / subcommand token).
99 void consumeUntil(const iterator& i)
100 {
101 m_currentFirstArg = i.index();
102 }
103
104 private:
105 std::vector<std::wstring> m_args;
106 size_t m_currentFirstArg = 0;
107 };
108 } // namespace wsl::windows::wslc