FreeNOS
Echo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Alexander Schrijver
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "Echo.h"
22 
23 Echo::Echo(int argc, char **argv)
24  : POSIXApplication(argc, argv)
25 {
26  parser().setDescription("Echo standard input to output");
27  parser().registerPositional("STRING", "text string to echo", 0);
28  parser().registerFlag('n', "no-newline", "If set, do not print new line character");
29 }
30 
32 {
33 }
34 
36 {
37  const Vector<Argument *> & positionals = arguments().getPositionals();
38  const char *no_newline = arguments().get("no-newline");
39 
40  // Loop positional arguments
41  for (Size i = 0; i < positionals.count(); i++)
42  printf("%s ", *(positionals[i]->getValue()));
43 
44  // Print newline
45  if (no_newline == NULL)
46  printf("\n");
47 
48  return Success;
49 }
ArgumentContainer::get
const char * get(const char *name) const
Get argument by name.
Definition: ArgumentContainer.cpp:49
string.h
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
Echo::~Echo
virtual ~Echo()
Destructor.
Definition: Echo.cpp:31
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
Echo::exec
virtual Result exec()
Execute the application.
Definition: Echo.cpp:35
Echo.h
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
stdio.h
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
Echo::Echo
Echo(int argc, char **argv)
Constructor.
Definition: Echo.cpp:23
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
stdlib.h
Vector< Argument * >
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39