[issue9399] Provide a 'print' action for argparse

2019-11-11 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Marking this as closed because there has been no activity or interest for over 
five years.

If anyone wants to revive this and write a PR, feel free to reopen.  The core 
idea is plausible, but this doesn't seem to be a recurring need.

--
resolution:  -> out of date
stage: test needed -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2019-08-29 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee: bethard -> rhettinger
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2019-04-27 Thread Mark Lawrence


Change by Mark Lawrence :


--
nosy:  -BreamoreBoy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2014-07-19 Thread paul j3

paul j3 added the comment:

As Steven notes, the patch lacks tests.  It also lacks documentation.  

The 2 things that this class does different from 'version' are
- write without passing the text through 'textwrap'
- write to a user defined file

There is a difference in opinion between Éric and Steven as to whether the 
class should use write directly or use the HelpFormatter.

I don't think it needs further action at this time.  There doesn't seem to be a 
lot of interest in it.  Also there are a number of ways of accomplishing the 
task without adding an Action class.

- the class is a simple adaptation of the 'version' class

- a user defined class works just as well

- 'version' with Raw formatter, and shell redirection also works

- it is also easy to write the text to a file after parse_args.

I've attached a file that illustrates a number of these alternatives.  It 
includes a 'callable' class.
-

The discussion got me thinking about a selective version of the RAW formatting, 
analogous to the HTML pre tag.  With this the user could mark a given text 
(description, epilog, help, version etc) as pre-formatted, without having to 
change the formatter_class.

parser.add_argument('--license', action='version',
version=Pre(formatted_text))

I probably should submit that in a new issue.

--
Added file: http://bugs.python.org/file35997/try_write.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2014-07-16 Thread Mark Lawrence

Mark Lawrence added the comment:

@Paul what is your take on this, other opinions seem positive?

--
nosy: +BreamoreBoy, paul.j3
versions: +Python 3.5 -Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2014-07-16 Thread Berker Peksag

Changes by Berker Peksag berker.pek...@gmail.com:


--
nosy: +berker.peksag

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2011-12-15 Thread Denilson Figueiredo de Sá

Changes by Denilson Figueiredo de Sá denilso...@gmail.com:


--
nosy: +denilsonsa

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2011-02-09 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

Argparse's wrapping behavior is determined by the formatter_class:

http://docs.python.org/library/argparse.html#formatter-class

Is it reasonable to assume that if you're wrapping your own messages you're 
already specifying formatter_class=argparse.RawTextHelpFormatter? If so, then 
perhaps the message should be printed via something like:

formatter = parser._get_formatter()
formatter.add_text(self.message)
file.write(formatter.format_help())

This is what we do in _VersionAction, so I guess it's probably what we should 
do here.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2011-02-03 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Thanks for the new patch.  It does not apply cleanly to the py3k branch, but 
that’s very easily corrected.


self.file.write(self.message)
self.file.write('\n')

Could be replaced by
print(self.message, file=self.file)

print will use the right EOL for the platform and is just coooler.


By the way, self.file should not be rebound in __call__:
if self.file is None:
self.file = _sys.stdout
→
file = (self.file if self.file is not None else _sys.stdout)


 Second, there are cases where you want whitespaces to be preserved,
 like printing out the license of your program (as I mentioned in my
 first post) Just look at this BSD license: [...]
 This is just plain ugly.

Agreed, but I did not suggest that the output be like that.  In my previous 
message, I suggested that argparse could replace \n with spaces and do its own 
line wrapping, to adapt to terminal length while avoiding chunking ugliness.  
That’s Steven’s call anyway, I don’t have any strong feeling in favor of 
preserving whitespace or rewrapping.  If argparse wraps lines to the terminal 
width in other cases (like with epilog text), I think it should do so here too.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-12-22 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

Thinking again about that, what’s wrong with argparse replacing \n with spaces 
and doing its own line wrapping?

--
versions: +Python 3.3 -Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-12-22 Thread Dennis Malcorps

Dennis Malcorps dennis.malco...@googlemail.com added the comment:

I totally forgot about this patch, sorry... Due to university and another 
python project I am working on I didn't find the time to look at the test 
suite. I would really appreciate it if someone else could do this ;-)

Anyway, I added the changes proposed by Steven Bethard and Éric Araujo 
regarding the default argument values. (patch made against trunk)

@Éric:
Well, first there is currently only the 'version' action which does this and 
imho it just looks strange if you try to print something else than a version 
info with it.
Second, there are cases where you want whitespaces to be preserved, like 
printing out the license of your program (as I mentioned in my first post) Just 
look at this BSD license:

Copyright (c) 2010, Dennis Malcorps dennis.malco...@gmail.com Redistribution
and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met: 1. Redistributions
of source code must retain the above copyright notice, this list of conditions  
 
and the following disclaimer. 2. Redistributions in binary form must reproduce  
 
the above copyright notice, this list of conditions and the following   
 
disclaimer in the documentation and/or other materials provided with the
 
distribution. 3. The name of the author may not be used to endorse or promote   
 
products derived from this software without specific prior written permission.  
 
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS AND ANY EXPRESS OR IMPLIED  
 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO  
 
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.


This is just plain ugly.

--
Added file: http://bugs.python.org/file20140/argparse.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-11-21 Thread Éric Araujo

Éric Araujo mer...@netwok.org added the comment:

sys.std* should not be used as default values in a function definition, because 
they may be rebound to other objects.  The usual idiom is to have None as 
default value and check it at call time.

The patch also needs tests and docs.

(FTR, the example for callable in this report was wrong: First, the message 
argument was missing in the lambda, second, there was no need for a lambda in 
the first place :)

--
nosy: +eric.araujo
stage:  - unit test needed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-08-01 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

The patch looks basically right. A few minor issues:

* message=None, should probably be message,, that is, message should not be 
allowed to default to None - I can't see any use case for this action without a 
message. I believe this means the body of __call__ can be simplified to::

  self.file.write(self.message)
  self.file.write(\n)
  parser.exit()

* The other thing the patch needs is to update the test suite to add tests to 
make sure this behavior works. Take a look at test_argparse.py for how to do 
that.

* The last thing is that to have the greatest chance of having someone check 
this in, you'll want to make your patch against Python trunk as explained here:

http://www.python.org/dev/faq/#how-do-i-get-a-checkout-of-the-repository-read-only-or-read-write
http://www.python.org/dev/faq/#how-to-make-a-patch

Thanks!

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-30 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

The equivalent to optparse callback is basically to define __call__ in a 
subclass of Action. Pretty much the same amount of work because they both have 
complicated parameters.

The callable I (not fully confidently) proposed would just be a shorthand for 
defining __call__ in a subclass of Action when you don't care about any of the 
other command line info.

I guess, without further use cases for callable and given that you can 
subclass Action for other use cases, let's just do the action='write' version. 
Feel free to supply a patch, or I will when I get some time for argparse again.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-30 Thread Dennis Malcorps

Dennis Malcorps dennis.malco...@googlemail.com added the comment:

Here is a patch that adds a 'write' action to argparse. Usage example:

 parser = argparse.ArgumentParser()
 parser.add_argument(--license, action=write, message=This file\nis 
 licensed under \t GPL)
 parser.parse_args(['--license'])
This file
is licensed underGPL

A linebreak will be added after the message. The Output can be redirected with 
the optional 'file=' argument (it defaults to sys.stdout) The parser will then 
exit. 

This is my first patch ever written, so don't be too harsh if it's utter 
garbage^^

--
keywords: +patch
Added file: http://bugs.python.org/file18268/argparse.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-29 Thread Georg Brandl

Georg Brandl ge...@python.org added the comment:

Sounds like a good addition to me.

--
assignee:  - bethard
nosy: +georg.brandl

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-29 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

Should this print to stdout or stderr? I wonder if the API should allow either, 
and instead look like:

parser.add_argument('--license', action='write', message='...', file=sys.stdout)

Where sys.stdout would be the default for the file= argument. The action would 
then just literally call file.write(message), so the behavior would be pretty 
easy to explain.

Of course, at that point it makes me wonder if maybe it wouldn't just be better 
to have an easy way to have some arbitrary function called without having to 
subclass Action, e.g.:

parser.add_argument('--license', action='call', callable=lambda: 
sys.stdout.write(message))

Basically this would be a shorthand for subclassing Action when you don't need 
any information about the command line.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-29 Thread Dennis Malcorps

Dennis Malcorps dennis.malco...@googlemail.com added the comment:

 parser.add_argument('--license', action='write', message='...', 
 file=sys.stdout)
The ability to redirect the output would be a nice addition.

 parser.add_argument('--license', action='call', callable=lambda: 
 sys.stdout.write(message)) 
optparse already has a 'callable' action, I had the impression it was left out 
of argparse on purpose, even tough I couldn't imagine why...

Either way is fine for me.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-28 Thread Dennis Malcorps

New submission from Dennis Malcorps dennis.malco...@googlemail.com:

Currently argparse has a 'version' action which can be triggered by user 
defined options which prints out a custom string.

parser.add_argument(--version, action=version, version=test 1.2.3)

Since the 'version' action can be added multiple times, it can be used to 
output different kinds of information, like the program's license.

parser.add_argument(--license, action=version, version=This file is 
licensed under GPL [a huge amount of text])

The only drawback is that linebreaks are substituted with a normal space. So I 
propose a 'print' action (perhaps as a replacement for 'version'?) which 
respects whitespace characters.

parser.add_argument(--version, action=print, message=test 1.2.3)
parser.add_argument(--license, action=print, message=This file is licensed 
under GPL [a huge amount of text, now properly formatted!])
parser.add_argument(--insult-me, action=print, message=You sick *peep* , 
*peep* yourself in *peep*)

Currently, the only solution is to create a custom action which is IMHO a bit 
overkill for just printing a simple string to stdout.

--
components: Library (Lib)
messages: 111824
nosy: travistouchdown
priority: normal
severity: normal
status: open
title: Provide a 'print' action for argparse
type: feature request
versions: Python 2.7, Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9399] Provide a 'print' action for argparse

2010-07-28 Thread R. David Murray

Changes by R. David Murray rdmur...@bitdance.com:


--
nosy: +bethard
versions:  -Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9399
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com