Custom Command Workflow
Designing Flexible Command Processing Systems
Creating a custom command workflow allows developers to build more sophisticated and interactive command-line applications with advanced functionality and user experience.
Command Processing Architecture
graph TD
A[User Input] --> B{Command Parser}
B --> C{Command Validator}
C --> |Valid| D[Command Executor]
C --> |Invalid| E[Error Handler]
D --> F[Result Generator]
E --> G[User Guidance]
Command Registration Mechanism
Component |
Responsibility |
Key Features |
Command Registry |
Store available commands |
Dynamic registration |
Validator |
Check command syntax |
Input verification |
Executor |
Implement command logic |
Modular design |
Error Handler |
Manage processing errors |
Informative feedback |
Comprehensive Command Workflow Implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_COMMANDS 10
#define MAX_COMMAND_LENGTH 50
typedef struct {
char name[MAX_COMMAND_LENGTH];
int (*handler)(int argc, char *argv[]);
char *description;
} Command;
Command commands[MAX_COMMANDS];
int command_count = 0;
int help_command(int argc, char *argv[]) {
printf("Available commands:\n");
for (int i = 0; i < command_count; i++) {
printf("- %s: %s\n", commands[i].name, commands[i].description);
}
return 0;
}
int register_command(const char *name,
int (*handler)(int argc, char *argv[]),
const char *description) {
if (command_count >= MAX_COMMANDS) {
fprintf(stderr, "Maximum commands reached\n");
return -1;
}
strncpy(commands[command_count].name, name, MAX_COMMAND_LENGTH);
commands[command_count].handler = handler;
commands[command_count].description = strdup(description);
command_count++;
return 0;
}
Command* find_command(const char *name) {
for (int i = 0; i < command_count; i++) {
if (strcmp(commands[i].name, name) == 0) {
return &commands[i];
}
}
return NULL;
}
int process_command(int argc, char *argv[]) {
if (argc < 2) {
help_command(argc, argv);
return -1;
}
Command *cmd = find_command(argv[1]);
if (cmd) {
return cmd->handler(argc - 1, argv + 1);
}
fprintf(stderr, "Unknown command: %s\n", argv[1]);
help_command(argc, argv);
return -1;
}
// Example custom commands
int create_command(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: create <filename>\n");
return -1;
}
FILE *file = fopen(argv[1], "w");
if (!file) {
perror("Error creating file");
return -1;
}
fclose(file);
printf("File %s created successfully\n", argv[1]);
return 0;
}
int main() {
// Register commands
register_command("help", help_command, "Display available commands");
register_command("create", create_command, "Create a new file");
// Example usage simulation
char *test_args1[] = {"program", "create", "test.txt"};
char *test_args2[] = {"program", "unknown"};
process_command(3, test_args1);
process_command(2, test_args2);
return 0;
}
Advanced Workflow Techniques
- Implement command auto-completion
- Create plugin-based command systems
- Support nested and complex commands
- Integrate comprehensive logging
Key Design Principles
- Modular command registration
- Flexible error handling
- Extensible architecture
- User-friendly interface
LabEx recommends developing adaptable command processing systems that can evolve with application requirements.