### Install argparse R package Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Demonstrates how to install the latest version of the 'argparse' package from CRAN and the development version from GitHub. ```R install.packages("argparse") remotes::install_github("trevorld/r-argparse") ``` -------------------------------- ### Mutually Exclusive Groups with argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Demonstrates how to create mutually exclusive argument groups, ensuring that only one argument from the group can be provided at a time. Includes examples of parsing valid and invalid combinations. ```R parser = ArgumentParser(prog='PROG') group = parser$add_mutually_exclusive_group() group$add_argument('--foo', action='store_true') group$add_argument('--bar', action='store_false') parser$parse_args('--foo') parser$parse_args('--bar') # Example of an error case: # parser$parse_args(c('--foo', '--bar')) ``` -------------------------------- ### Parse Arguments with R argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Parses command-line arguments using an R argparse parser. This example demonstrates how to pass arguments and access the parsed values. ```R parser$parse_args(c('--foo', 'b', '--baz', 'Z')) ``` -------------------------------- ### Basic Argument Parsing with argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Shows how to create an ArgumentParser object, add positional and optional arguments, print usage, and parse arguments from a character vector. ```R library("argparse") parser <- ArgumentParser(description='Process some integers') parser$add_argument('integers', metavar='N', type="integer", nargs='+', help='an integer for the accumulator') parser$add_argument('--sum', dest='accumulate', action='store_const', const='sum', default='max', help='sum the integers (default: find the max)') parser$print_help() args <- parser$parse_args(c("--sum", "1", "2", "3") ) accumulate_fn <- get(args$accumulate) print(accumulate_fn(args$integers)) ``` -------------------------------- ### Sub-commands with argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Shows how to implement sub-commands using argparse, allowing for different sets of arguments to be handled based on a sub-command specified on the command line. ```R # create the top-level parser parser = ArgumentParser(prog='PROG') parser$add_argument('--foo', action='store_true', help='foo help') subparsers = parser$add_subparsers(help='sub-command help') # create the parser for the "a" command parser_a = subparsers$add_parser('a', help='a help') parser_a$add_argument('bar', type='integer', help='bar help') # create the parser for the "b" command parser_b = subparsers$add_parser('b', help='b help') parser_b$add_argument('--baz', choices='XYZ', help='baz help') # parse some argument lists parser$parse_args(c('a', '12')) ``` -------------------------------- ### Argument Groups in argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Illustrates how to use argument groups to organize related command-line arguments, including adding descriptions to groups. ```R parser = ArgumentParser(prog='PROG', add_help=FALSE) group1 = parser$add_argument_group('group1', 'group1 description') group1$add_argument('foo', help='foo help') group2 = parser$add_argument_group('group2', 'group2 description') group2$add_argument('--bar', help='bar help') parser$print_help() ``` -------------------------------- ### Print Help Message with R argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Prints the help message for an R argparse parser. This is useful for users to understand the available arguments and their usage. ```R parser$print_help() ``` -------------------------------- ### Print Help Message for Sub-command 'b' with Choices in R argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Prints the help message for a sub-command ('b') that includes an optional argument with predefined choices ('X', 'Y', 'Z') in R argparse. ```R parser_b$print_help() ``` -------------------------------- ### Print Help Message for Sub-command 'a' in R argparse Source: https://github.com/trevorld/r-argparse/blob/master/README.rst Prints the help message for a specific sub-command ('a') within an R argparse structure. This helps in understanding the arguments specific to that sub-command. ```R parser_a$print_help() ``` -------------------------------- ### Logical Type with Store Action in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Adds a warning when 'logical' type is used with 'store' action in add_argument, recommending 'store_true' or 'store_false' for clarity. ```R # Conceptual example triggering the warning # parser$add_argument('--flag', type='logical', action='store', help='A logical flag') ``` -------------------------------- ### Support for Formatter Class in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Introduces support for the 'formatter_class' argument in ArgumentParser, allowing customization of help message formatting. ```R # Conceptual example with formatter_class # parser <- ArgumentParser(formatter_class='argparse::RawTextHelpFormatter') # parser$add_argument('--help-text', help='Help text with raw formatting') ``` -------------------------------- ### Add Version Action in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Introduces the capability to display the program's version using the 'version' action in the add_argument function. ```R parser$add_argument('--version', action='version', version='1.0.7') ``` -------------------------------- ### Support for Choices Argument in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Adds initial support for the 'choices' argument in parser$add_argument, allowing specification of allowed values for an argument. ```R # Conceptual example with choices # parser$add_argument('--mode', choices=c('fast', 'slow'), help='Execution mode') ``` -------------------------------- ### Find Python 2.7 Binary in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Updates find_python_cmd to also search for a 'python2.7' binary, improving compatibility with older Python versions. ```R # Conceptual check for python2.7 # python2_cmd <- findpython::find_python_cmd(version='2.7') # print(paste('Found Python 2.7:', python2_cmd)) ``` -------------------------------- ### Handle Unicode Arguments in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Enhances the package's ability to handle Unicode characters in arguments and options, improving internationalization support. ```R # Example demonstrating Unicode argument handling (conceptual) # parser$add_argument('--unicode-option', type='character', help='An option with Unicode characters') ``` -------------------------------- ### Find Python Command in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Utilizes the findpython::find_python_cmd function for more robust detection of Python binaries. ```R # Conceptual usage of find_python_cmd # python_cmd <- findpython::find_python_cmd() # print(paste('Found Python:', python_cmd)) ``` -------------------------------- ### Set NULL Default in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Enables explicit setting of a NULL default value for arguments using add_argument. ```R # Conceptual example setting NULL default # parser$add_argument('--optional', default=NULL, help='An optional argument') ``` -------------------------------- ### Pass Character Vector to Metavar in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Allows passing a character vector to the 'metavar' argument in add_argument, providing more flexibility in argument display. ```R # Conceptual example of using character vector for metavar # parser$add_argument('--files', metavar=c('FILE1', 'FILE2'), help='Input files') ``` -------------------------------- ### Interactive vs Non-interactive Parse Behavior in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Modifies the behavior of parse_args in interactive R sessions to throw an error instead of quitting after displaying a help message. ```R # Conceptual example of parse_args behavior # if (interactive()) { # tryCatch({ # parser$parse_args() # }, error = function(e) { # print('Error displayed instead of quitting') # }) # } else { # parser$parse_args() # } ``` -------------------------------- ### Set Integer Type in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Fixes a bug related to setting the 'integer' type for arguments using the add_argument function. ```R # Conceptual example of setting argument type to integer # parser$add_argument('--count', type='integer', help='A count value') ``` -------------------------------- ### Handle Default Vector in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Fixes a bug in parser$add_argument when a default vector is specified instead of a scalar value. ```R # Conceptual example with default vector # parser$add_argument('--items', default=c('a', 'b'), help='A list of items') ``` -------------------------------- ### Handle Python Errors in R-argparse Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md Ensures that error messages from Python scripts are passed on to the user when parsing arguments. ```R # Conceptual example of Python script execution and error handling # tryCatch({ # args <- parser$parse_args() # }, error = function(e) { # print(paste('Python script error:', e$message)) # }) ``` -------------------------------- ### Silence R Error Output Source: https://github.com/trevorld/r-argparse/blob/master/NEWS.md This code snippet demonstrates how to silence the default R error handler when running scripts non-interactively. It sets a custom error option to quit without outputting 'Execution halted', which is useful for cleaner script execution. ```R if (!interactive()) options(error=function(e) quit('no', status = 1, runLast = FALSE)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.