2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
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
)
31 fShortUsage(shortUsage
),
32 fLongUsage(longUsage
),
35 fShortUsage
.ReplaceAll("%command%", fName
);
36 fLongUsage
.ReplaceAll("%command%", fName
);
46 Command::Init(const char* programName
)
48 fShortUsage
.ReplaceAll("%program%", programName
);
49 fLongUsage
.ReplaceAll("%program%", programName
);
54 Command::PrintUsage(bool error
) const
56 fprintf(error
? stderr
: stdout
, "%s", fLongUsage
.String());
61 Command::PrintUsageAndExit(bool error
) const
68 // #pragma mark - CommandManager
71 /*static*/ CommandManager
*
72 CommandManager::Default()
74 static CommandManager
* manager
= new CommandManager
;
80 CommandManager::RegisterCommand(Command
* command
)
82 fCommands
.AddItem(command
);
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
);
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
);
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()