Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / pkgman / Command.cpp
blob274665078f6a8657a983a723e943cea291f168ea
1 /*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include "Command.h"
12 #include <stdio.h>
13 #include <stdlib.h>
16 static int
17 compare_commands_by_name(const Command* a, const Command* b)
19 return a->Name().Compare(b->Name());
23 // #pragma mark - Command
26 Command::Command(const BString& name, const BString& shortUsage,
27 const BString& longUsage, const BString& category)
29 fCommonOptions(),
30 fName(name),
31 fShortUsage(shortUsage),
32 fLongUsage(longUsage),
33 fCategory(category)
35 fShortUsage.ReplaceAll("%command%", fName);
36 fLongUsage.ReplaceAll("%command%", fName);
40 Command::~Command()
45 void
46 Command::Init(const char* programName)
48 fShortUsage.ReplaceAll("%program%", programName);
49 fLongUsage.ReplaceAll("%program%", programName);
53 void
54 Command::PrintUsage(bool error) const
56 fprintf(error ? stderr : stdout, "%s", fLongUsage.String());
60 void
61 Command::PrintUsageAndExit(bool error) const
63 PrintUsage(error);
64 exit(error ? 1 : 0);
68 // #pragma mark - CommandManager
71 /*static*/ CommandManager*
72 CommandManager::Default()
74 static CommandManager* manager = new CommandManager;
75 return manager;
79 void
80 CommandManager::RegisterCommand(Command* command)
82 fCommands.AddItem(command);
86 void
87 CommandManager::InitCommands(const char* programName)
89 for (int32 i = 0; Command* command = fCommands.ItemAt(i); i++)
90 command->Init(programName);
92 fCommands.SortItems(&compare_commands_by_name);
96 void
97 CommandManager::GetCommands(const char* prefix, CommandList& _commands)
99 for (int32 i = 0; Command* command = fCommands.ItemAt(i); i++) {
100 if (command->Name().StartsWith(prefix))
101 _commands.AddItem(command);
106 void
107 CommandManager::GetCommandsForCategory(const char* category,
108 CommandList& _commands)
110 for (int32 i = 0; Command* command = fCommands.ItemAt(i); i++) {
111 if (command->Category() == category)
112 _commands.AddItem(command);
117 CommandManager::CommandManager()
119 fCommands(20, true)