Invocation.h (1767B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <map> 5 #include <vector> 6 7 namespace AppInstaller::CLI 8 { 9 // Contains the raw command line arguments and functionality to iterate and consume them. 10 struct Invocation 11 { 12 Invocation(std::vector<std::string>&& args) : m_args(std::move(args)) {} 13 14 struct iterator 15 { 16 iterator(size_t arg, std::vector<std::string>& args) : m_arg(arg), m_args(args) {} 17 18 iterator(const iterator&) = default; 19 iterator& operator=(const iterator&) = default; 20 21 iterator operator++() { return { ++m_arg, m_args }; } 22 iterator operator++(int) { return { m_arg++, m_args }; } 23 iterator operator--() { return { --m_arg, m_args }; } 24 iterator operator--(int) { return { m_arg--, m_args }; } 25 26 bool operator==(const iterator& other) const { return m_arg == other.m_arg; } 27 bool operator!=(const iterator& other) const { return m_arg != other.m_arg; } 28 29 const std::string& operator*() const { return m_args[m_arg]; } 30 const std::string* operator->() const { return &(m_args[m_arg]); } 31 32 size_t index() const { return m_arg; } 33 34 private: 35 size_t m_arg; 36 std::vector<std::string>& m_args; 37 }; 38 39 size_t size() const { return m_args.size(); } 40 iterator begin() { return { m_currentFirstArg, m_args }; } 41 iterator end() { return { m_args.size(), m_args }; } 42 void consume(const iterator& i) { m_currentFirstArg = i.index() + 1; } 43 44 private: 45 std::vector<std::string> m_args; 46 size_t m_currentFirstArg = 0; 47 }; 48 }