What's the difference between a flag, an option, and an argument?

47,581

Solution 1

There are no consistent definitions of the terms "option", "argument", and "flag", and there is no central authority within the software development world that could enforce their usage. This happens with much terminology: after 30+ years of using the word "directory", I now have to deal with people using the word "folder" who have been confused by Microsoft's new-speak.

There are different ways that consensus definitions for terms can come about in programming. In the case of "argument"/"option"/"flag", canonical manuals and tutorials for programming languages have helped to enforce usage, as has the terms used in common libraries.

For instance, the things you put on the command line after a command are often called "arguments" to the command, by analogy with arguments to a function call, and this is probably partly because they are called "arguments" in the C manual (hence argc and argv). The argparse Python library also helps to enforce the term "argument". However, I have also seen them being called "parameters".

The term "option" is derived from "optional", which implies they can be left out. The getopt C library is one use of this term. But there is precedent for "options" that are not actually optional: for example, the argparse manual states that one can create a "required option" (although it also says that this is "generally considered bad form"). Options are often preceded by a single (-) or double (--, long-option) dash, but there are well-known commands that do not require or enforce dash usage for options (e.g., tar, ps, and dd). An option can itself take an argument (e.g., -w80 and --color=always), or occasionally multiple arguments.

"Flags" are, in my experience, the same as options, but usually do not take arguments themselves and essentially represent boolean on-off switches.

On a broader note, since every programmer has the option to try and look up some standard way of doing things and naming things, but can also reinvent the wheel without much extra cost, naming is never going to be consistent. And once you have documented your code, and it is clear what new meaning you have given to these words by giving examples, those names and meanings might just stick if there are enough people who pick them up from your code.

Solution 2

A flag is a type of option, an option of boolean type, and is always false by default (e.g. --verbose, --quiet, --all, --long, etc).

An option tells the function how to act (e.g. -a, -l, --verbose, --output , -name , -c , etc), whist an arguments tells the function what to act on/from (e.g. *, file1, hostname, database).

Share:
47,581

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years
    • ls -a (I consider -a an option)

    • sudo -u username (-u = option, username = arg)

    • chmod 664 my-dir (664 = option, my-dir = arg)

    I can't think of an example that might say "this is a flag" except perhaps when looking at dir listing:

    -r--------. 1 david david   3344 May 19 17:48 611056.pdf
    

    This has the "read flag" set for the owner, but that's all. What's to stop me from calling that a "read option"?

    I write and edit technical documentation, primarily in DocBook XML, and I'm looking for an explanation of the difference, which is consistent and accurate as possible. However, I'm already seeing a pattern forming:

    1. flags tend to be Booleans. e.g., setenforce 0
    2. options help define how a command should behave. Some may be optional.
    3. arguments tell commands what object to operate on.

    I could see myself combining flags and options (some options may have a dozen possible values, but Booleans only have two). Arguments appear sufficiently different to maintain them as such.

  • Anthon
    Anthon about 8 years
    So in your definition, in git pull git://bla/bla, the pull is an argument that tells gitwhat to work on.
  • Ezequiel Tolnay
    Ezequiel Tolnay about 8 years
    @Anthon no, there are operators (switches/flags/parameters), arguments, and commands. pull is a command. One can think of commands as extensions of the application's name: git pull <...> is equivalent to an application called git-pull. When the application has commands, each command makes the application do something completely different.
  • Anthon
    Anthon about 8 years
    The git people might call it a command, but that is not always what it is called e.g. for tar, using c for creating is called a function. And that is exactly the problem: there is no standardisation of terms, even if it looks like you think there is from your answer
  • Alessio
    Alessio about 8 years
    a flag doesn't have to be false by default. providing the flag on the command line just reverses its default state (on-->off, or off-->on). some people call that a toggle to distinguish it from a flag. others think the distinction is unimportant.
  • Marcel Besixdouze
    Marcel Besixdouze almost 4 years
    Just to add to this, I've seen various organizations enforce a distinction between "parameter" and "argument", e.g. developer.mozilla.org/en-US/docs/Glossary/Parameter "A parameter is a named variable passed into a function. Function arguments are the real values passed to the function."
  • Adrian McCarthy
    Adrian McCarthy over 3 years
    "there is no central authority" -- In the Unix/Linux world, there is the Open Group's Posix standard. Arguments are the tokens on the command line. The first argument is the utility name. Options are generally optional and modify the utility's default behavior. They are distinguished from other arguments by a leading -. The . An option may have an option argument. The remaining arguments are operands. Granted, not everything strictly follows Posix terminology, often for historical reasons. Flag and switch are informal jargon for a boolean-valued option.
  • The Quark
    The Quark over 2 years
    @MarcelBesixdouze And at the same time in the bash manpage it is written "A variable is a parameter denoted by a name." Although in the context of bash, "name" has the special restricted meaning of "A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore."