Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/CLI

A Wikiszótárból, a nyitott szótárból

🔹 What is CLI?

[szerkesztés]

Command Line Interface (CLI) is a way to interact with a program by typing text commands into a terminal or console. It’s:

  • Efficient for developers and system admins
  • Great for scripting and automation
  • Common in C++ projects, especially tools and utilities



🔹 Basic Input and Output (I/O)

[szerkesztés]

📌 Code:

[szerkesztés]
#include <iostream>

int main() {
    std::cout << "Enter any integer: " << std::endl;
    int num;
    std::cin >> num;
    std::cout << "The square of this number is:" << num*num << std::endl;
    return 0;
}

✅ Explanation:

[szerkesztés]
  • std::cout prints to the terminal (standard output).
  • std::cin takes input from the user (standard input).
  • The user enters an integer, and the program prints its square.



🔹 Creating Interactive Menus (User-Driven CLI)

[szerkesztés]

📌 Code:

[szerkesztés]
int choice;
do {
    std::cout << "1. Option 1\n2. Option 2\n3. Quit\nEnter your choice: ";
    std::cin >> choice;
    std::cout << "###################\n";

    switch (choice) {
        case 1: std::cout << "You have chosen option 1\n"; break;
        case 2: std::cout << "You have chosen option 2\n"; break;
        case 3: break; // Exit condition
        default: std::cout << "Invalid choice. Try again.\n"; break;
    }
} while (choice != 3);

✅ Explanation:

[szerkesztés]
  • Shows a simple menu using do-while.
  • Uses a switch to perform actions based on user input.
  • Continues until user selects option 3.



🔹 Command-Line Arguments (argc, argv)

[szerkesztés]

📌 Code:

[szerkesztés]
int main(int argc, char *argv[]) {
    std::cout << "Arguments count: " << argc << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << "Argument " << i << ": " << argv[i] << std::endl;
    }
}

✅ Explanation:

[szerkesztés]
  • argc: number of arguments passed (includes program name).

  • argv[]: array of C-style strings representing arguments.

  • Use:

    ./myprogram one two three
    

    Output:

    Argument 0: ./myprogram
    Argument 1: one
    Argument 2: two
    Argument 3: three



🔹 Flag-Style Arguments (e.g. -v, --help)

[szerkesztés]

This is common in professional CLI tools like gcc, ls, or curl.

📌 Code (with getopt.h):

[szerkesztés]
#include <iostream>
#include <getopt.h>

int main(int argc, char *argv[]) {
    char short_options[] = "hv";
    int opt;

    while ((opt = getopt(argc, argv, short_options)) != -1) {
        switch (opt) {
            case 'h':
                std::cout << "Usage: myprogram [options]\n\n"
                          << "Options:\n"
                          << "  -h    Print this help message\n"
                          << "  -v    Print the version number\n";
                break;
            case 'v':
                std::cout << "myprogram v1.0.0\n";
                break;
            default:
                std::cout << "Unknown option. Use '-h' for help.\n";
                break;
        }
    }

    if (argc == 1) {
        std::cout << "Use '-h' for usage information.\n";
    }
}

✅ Explanation:

[szerkesztés]
  • getopt processes options defined in short_options (-h and -v here).
  • -h shows help; -v shows version.
  • It loops over all provided options.

🛠️ To compile:

g++ myprogram.cpp -o myprogram

🧪 To run:

./myprogram -h

🔹 Summary / Takeaways

[szerkesztés]
Feature Purpose
std::cin / std::cout Simple user interaction
switch + loops Menus and command handling
argc, argv[] Access command-line parameters
getopt() Advanced parsing of flags and options



🧠 Pro Tip

[szerkesztés]

Combine interactive input and command-line args to build powerful, flexible CLI apps that adapt to how the user wants to interact—either in bulk (via args) or manually (via prompts).