def test_format_usage(self): """Test :func:`humanfriendly.usage.format_usage()`.""" # Test that options are highlighted. usage_text = "Just one --option" formatted_text = format_usage(usage_text) assert len(formatted_text) > len(usage_text) assert formatted_text.startswith("Just one ") # Test that the "Usage: ..." line is highlighted. usage_text = "Usage: humanfriendly [OPTIONS]" formatted_text = format_usage(usage_text) assert len(formatted_text) > len(usage_text) assert usage_text in formatted_text assert not formatted_text.startswith(usage_text) # Test that meta variables aren't erroneously highlighted. usage_text = ( "--valid-option=VALID_METAVAR\n" "VALID_METAVAR is bogus\n" "INVALID_METAVAR should not be highlighted\n" ) formatted_text = format_usage(usage_text) formatted_lines = formatted_text.splitlines() # Make sure the meta variable in the second line is highlighted. assert ANSI_CSI in formatted_lines[1] # Make sure the meta variable in the third line isn't highlighted. assert ANSI_CSI not in formatted_lines[2]
def usage(usage_text): """ Print a human friendly usage message to the terminal. :param text: The usage message to print (a string). This function does two things: 1. If :data:`sys.stdout` is connected to a terminal (see :func:`connected_to_terminal()`) then the usage message is formatted using :func:`.format_usage()`. 2. The usage message is shown using a pager (see :func:`show_pager()`). """ if terminal_supports_colors(sys.stdout): usage_text = format_usage(usage_text) show_pager(usage_text)
def usage(usage_text): """ Print a human friendly usage message to the terminal. :param text: The usage message to print (a string). This function does two things: 1. If :data:`sys.stdout` is connected to a terminal (see :func:`connected_to_terminal()`) then the usage message is formatted using :func:`.format_usage()`. 2. The usage message is shown using a pager (see :func:`show_pager()`). """ if connected_to_terminal(sys.stdout): usage_text = format_usage(usage_text) show_pager(usage_text)