1 #pragma once
2 
3 /** types and utilities for input and output
4  * of commands
5  */
6 
7 #include "arglist.h"
8 
9 class Completion;
10 
11 /* A path in the object tree */
12 using Path = ArgList;
13 
14 /* Types for I/O with the user */
15 using Output = std::ostream&;
16 
17 /** The Input for a command consists of a command name
18  * (known as argv[0] in a C main()) and of arguments (argv[1] up to
19  * argv[argc] in a C main()).
20  *
21  * This class is in transition and not yet in its final state!
22  * Currently, this class behaves as the ArgList. But later, ArgList will become
23  * protected and so one can only use >> to access the arguments
24  *
25  * This means that currently, the base class holds the entire argv. Later, the
26  * base class will only hold the arguments and not the command name and
27  * furthermore the ArgList parent class will become private.
28  */
29 class Input : public ArgList {
30 public:
31     Input(const std::string command, const Container &c = {})
ArgList(c)32         : ArgList(c), command_(std::make_shared<std::string>(command)) {}
33 
Input(const std::string command,Container::const_iterator from,Container::const_iterator to)34     Input(const std::string command, Container::const_iterator from, Container::const_iterator to)
35         : ArgList(from, to), command_(std::make_shared<std::string>(command)) {}
36 
37     //! create a new Input but drop already parsed arguments
Input(const Input & other)38     Input(const Input& other)
39         : ArgList(other.toVector()), command_(other.command_) {}
40 
command()41     const std::string& command() const { return *command_; }
42 
43     Input &operator>>(std::string &val) override;
44 
45     //! construct a new Input where the first (current) arg is the command
46     Input fromHere();
47 
48     //! Replace every occurence of 'from' by 'to'
49     //! @note this includes the command itself
50     void replace(const std::string &from, const std::string &to);
51 
52 protected:
53     //! Command name
54     //! A shared pointer to avoid copies when passing Input around
55     //! @note The C-style compatibility layer DEPENDS on the shared_ptr!
56     std::shared_ptr<std::string> command_;
57 };
58