How do I pass a unique_ptr argument to a constructor or a function?

Here are the possible ways to take a unique pointer as an argument, as well as their associated meaning. (A) By Value Base(std::unique_ptr<Base> n) : next(std::move(n)) {} In order for the user to call this, they must do one of the following: Base newBase(std::move(nextBase)); Base fromTemp(std::unique_ptr<Base>(new Base(…)); To take a unique pointer by value means … Read more

How to write a bash script that takes optional input arguments?

You could use the default-value syntax: somecommand ${1:-foo} The above will, as described in Bash Reference Manual – 3.5.3 Shell Parameter Expansion [emphasis mine]: If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. If you only want to substitute a default value if the parameter … Read more

Which exception should I raise on bad/illegal argument combinations in Python?

I would just raise ValueError, unless you need a more specific exception.. def import_to_orm(name, save=False, recurse=False): if recurse and not save: raise ValueError(“save must be True if recurse is True”) There’s really no point in doing class BadValueError(ValueError):pass – your custom class is identical in use to ValueError, so why not use that?

Is there a better way to do optional function parameters in JavaScript? [duplicate]

Your logic fails if optionalArg is passed, but evaluates as false – try this as an alternative if (typeof optionalArg === ‘undefined’) { optionalArg = ‘default’; } Or an alternative idiom: optionalArg = (typeof optionalArg === ‘undefined’) ? ‘default’ : optionalArg; Use whichever idiom communicates the intent best to you!

How do I parse command line arguments in Bash?

Bash Space-Separated (e.g., –option argument) cat >/tmp/demo-space-separated.sh <<‘EOF’ #!/bin/bash POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -e|–extension) EXTENSION=”$2″ shift # past argument shift # past value ;; -s|–searchpath) SEARCHPATH=”$2″ shift # past argument shift # past value ;; –default) DEFAULT=YES shift # past argument ;; -*|–*) echo “Unknown option $1” exit … Read more