Using argparse to pass in number of output lines
I'm writing a program that outputs a list. Let's say this list can have hundreds of items. I want to use argparse to pass in an option for how many items are shown in the output, and default to 15 lines. How do I get the option to be passed through as a variable in my function?
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
# ...omitted code for other options
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args(argv)
def scramble_words():
"""
Shuffle words in new_list
Print reordered words by newline
"""
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
python argparse
add a comment |
I'm writing a program that outputs a list. Let's say this list can have hundreds of items. I want to use argparse to pass in an option for how many items are shown in the output, and default to 15 lines. How do I get the option to be passed through as a variable in my function?
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
# ...omitted code for other options
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args(argv)
def scramble_words():
"""
Shuffle words in new_list
Print reordered words by newline
"""
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
python argparse
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. Thescramble_words
definition should obviously be changed to accept an argument (perhaps optional).
– tripleee
Jan 1 at 23:33
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
1
During debugging it's a good idea to print the result ofparse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.
– hpaulj
Jan 2 at 2:34
add a comment |
I'm writing a program that outputs a list. Let's say this list can have hundreds of items. I want to use argparse to pass in an option for how many items are shown in the output, and default to 15 lines. How do I get the option to be passed through as a variable in my function?
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
# ...omitted code for other options
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args(argv)
def scramble_words():
"""
Shuffle words in new_list
Print reordered words by newline
"""
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
python argparse
I'm writing a program that outputs a list. Let's say this list can have hundreds of items. I want to use argparse to pass in an option for how many items are shown in the output, and default to 15 lines. How do I get the option to be passed through as a variable in my function?
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
# ...omitted code for other options
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args(argv)
def scramble_words():
"""
Shuffle words in new_list
Print reordered words by newline
"""
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
python argparse
python argparse
edited Jan 2 at 4:02
Stephen Rauch
30k153758
30k153758
asked Jan 1 at 22:46
J. ChengJ. Cheng
165
165
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. Thescramble_words
definition should obviously be changed to accept an argument (perhaps optional).
– tripleee
Jan 1 at 23:33
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
1
During debugging it's a good idea to print the result ofparse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.
– hpaulj
Jan 2 at 2:34
add a comment |
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. Thescramble_words
definition should obviously be changed to accept an argument (perhaps optional).
– tripleee
Jan 1 at 23:33
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
1
During debugging it's a good idea to print the result ofparse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.
– hpaulj
Jan 2 at 2:34
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. The scramble_words
definition should obviously be changed to accept an argument (perhaps optional).– tripleee
Jan 1 at 23:33
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. The scramble_words
definition should obviously be changed to accept an argument (perhaps optional).– tripleee
Jan 1 at 23:33
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
1
1
During debugging it's a good idea to print the result of
parse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.– hpaulj
Jan 2 at 2:34
During debugging it's a good idea to print the result of
parse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.– hpaulj
Jan 2 at 2:34
add a comment |
2 Answers
2
active
oldest
votes
Another option is to use the Click package for creating command line options. I personally find it more intuitive.
import click
@click.command()
@click.option('-n', '--noutput')
def driver(noutput):
print(noutput)
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
driver()
If you want to stick with argparse, here is how you can pass the argument to your function. I made some assumption on how you might be using the code.
import sys
import argparse
import random
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args()
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
args = get_args(sys.argv)
scramble_words(args.noutput)
[:--noutput]
is that some new indexing syntax? :) A special double negative?
– hpaulj
Jan 2 at 0:21
Should probably be just[:noutput]
.
– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
You probably put the--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,
– tripleee
Jan 2 at 4:46
1
I wouldn't bother withoptparse
, it's basically just even clunkier and more hostile thanargparse
. The ostensible improvements inargparse
are not in areas which affect beginners. I hesitantly condoneclick
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hopeclick
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.
– tripleee
Jan 2 at 4:52
|
show 1 more comment
You are not showing how you are calling either of these functions, but something like
def scramble_words(lst, n=15):
"""
Shuffle words in lst
Print n reordered words by newline
"""
random.shuffle(lst)
print()
print("nn".join(lst[:n]))
# ...
args = get_args()
scramble_words(new_list, args.noutput)
A better design would have the caller do the printing, and probably only print a single newline between words.
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999537%2fusing-argparse-to-pass-in-number-of-output-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Another option is to use the Click package for creating command line options. I personally find it more intuitive.
import click
@click.command()
@click.option('-n', '--noutput')
def driver(noutput):
print(noutput)
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
driver()
If you want to stick with argparse, here is how you can pass the argument to your function. I made some assumption on how you might be using the code.
import sys
import argparse
import random
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args()
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
args = get_args(sys.argv)
scramble_words(args.noutput)
[:--noutput]
is that some new indexing syntax? :) A special double negative?
– hpaulj
Jan 2 at 0:21
Should probably be just[:noutput]
.
– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
You probably put the--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,
– tripleee
Jan 2 at 4:46
1
I wouldn't bother withoptparse
, it's basically just even clunkier and more hostile thanargparse
. The ostensible improvements inargparse
are not in areas which affect beginners. I hesitantly condoneclick
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hopeclick
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.
– tripleee
Jan 2 at 4:52
|
show 1 more comment
Another option is to use the Click package for creating command line options. I personally find it more intuitive.
import click
@click.command()
@click.option('-n', '--noutput')
def driver(noutput):
print(noutput)
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
driver()
If you want to stick with argparse, here is how you can pass the argument to your function. I made some assumption on how you might be using the code.
import sys
import argparse
import random
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args()
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
args = get_args(sys.argv)
scramble_words(args.noutput)
[:--noutput]
is that some new indexing syntax? :) A special double negative?
– hpaulj
Jan 2 at 0:21
Should probably be just[:noutput]
.
– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
You probably put the--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,
– tripleee
Jan 2 at 4:46
1
I wouldn't bother withoptparse
, it's basically just even clunkier and more hostile thanargparse
. The ostensible improvements inargparse
are not in areas which affect beginners. I hesitantly condoneclick
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hopeclick
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.
– tripleee
Jan 2 at 4:52
|
show 1 more comment
Another option is to use the Click package for creating command line options. I personally find it more intuitive.
import click
@click.command()
@click.option('-n', '--noutput')
def driver(noutput):
print(noutput)
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
driver()
If you want to stick with argparse, here is how you can pass the argument to your function. I made some assumption on how you might be using the code.
import sys
import argparse
import random
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args()
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
args = get_args(sys.argv)
scramble_words(args.noutput)
Another option is to use the Click package for creating command line options. I personally find it more intuitive.
import click
@click.command()
@click.option('-n', '--noutput')
def driver(noutput):
print(noutput)
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
driver()
If you want to stick with argparse, here is how you can pass the argument to your function. I made some assumption on how you might be using the code.
import sys
import argparse
import random
def get_args(argv = None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-n',
'--noutput',
default = 15,
type = int,
help = 'Number of lines in output'
)
return parser.parse_args()
def scramble_words(noutput):
"""
Shuffle words in new_list
Print reordered words by newline
"""
new_list=['a', 'b', 'c', 'd', 'e']
random.shuffle(new_list)
print( )
print("nn".join(new_list[:--noutput]))
if __name__ == "__main__":
args = get_args(sys.argv)
scramble_words(args.noutput)
answered Jan 1 at 23:48
faisalfaisal
1836
1836
[:--noutput]
is that some new indexing syntax? :) A special double negative?
– hpaulj
Jan 2 at 0:21
Should probably be just[:noutput]
.
– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
You probably put the--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,
– tripleee
Jan 2 at 4:46
1
I wouldn't bother withoptparse
, it's basically just even clunkier and more hostile thanargparse
. The ostensible improvements inargparse
are not in areas which affect beginners. I hesitantly condoneclick
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hopeclick
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.
– tripleee
Jan 2 at 4:52
|
show 1 more comment
[:--noutput]
is that some new indexing syntax? :) A special double negative?
– hpaulj
Jan 2 at 0:21
Should probably be just[:noutput]
.
– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
You probably put the--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,
– tripleee
Jan 2 at 4:46
1
I wouldn't bother withoptparse
, it's basically just even clunkier and more hostile thanargparse
. The ostensible improvements inargparse
are not in areas which affect beginners. I hesitantly condoneclick
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hopeclick
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.
– tripleee
Jan 2 at 4:52
[:--noutput]
is that some new indexing syntax? :) A special double negative?– hpaulj
Jan 2 at 0:21
[:--noutput]
is that some new indexing syntax? :) A special double negative?– hpaulj
Jan 2 at 0:21
Should probably be just
[:noutput]
.– tripleee
Jan 2 at 0:23
Should probably be just
[:noutput]
.– tripleee
Jan 2 at 0:23
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
Thanks for the help here! I was able to get it working from both your and @tripleee's inputs. Click package definitely seems more intuitive; I need to try optparse as well to get a better understanding here. Really appreciate it!!
– J. Cheng
Jan 2 at 0:26
1
1
You probably put the
--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,– tripleee
Jan 2 at 4:46
You probably put the
--
assuming the dashes were part of the variable's name. They are wrong and it just coincidentally happens to work anyway, probably because Python parses two consecutive minuses into effectively a redundant plus. "Best practice" to omit them is a gross understatement IMNSHO,– tripleee
Jan 2 at 4:46
1
1
I wouldn't bother with
optparse
, it's basically just even clunkier and more hostile than argparse
. The ostensible improvements in argparse
are not in areas which affect beginners. I hesitantly condone click
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hope click
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.– tripleee
Jan 2 at 4:52
I wouldn't bother with
optparse
, it's basically just even clunkier and more hostile than argparse
. The ostensible improvements in argparse
are not in areas which affect beginners. I hesitantly condone click
as a somewhat more user-friendly alternative, though there are several competing third-party libraries and a bit of a shakeout going on between them. Let's earnestly hope click
or something like it comes out as a clear winner and ideally eventually a part of the de facto standard library.– tripleee
Jan 2 at 4:52
|
show 1 more comment
You are not showing how you are calling either of these functions, but something like
def scramble_words(lst, n=15):
"""
Shuffle words in lst
Print n reordered words by newline
"""
random.shuffle(lst)
print()
print("nn".join(lst[:n]))
# ...
args = get_args()
scramble_words(new_list, args.noutput)
A better design would have the caller do the printing, and probably only print a single newline between words.
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
add a comment |
You are not showing how you are calling either of these functions, but something like
def scramble_words(lst, n=15):
"""
Shuffle words in lst
Print n reordered words by newline
"""
random.shuffle(lst)
print()
print("nn".join(lst[:n]))
# ...
args = get_args()
scramble_words(new_list, args.noutput)
A better design would have the caller do the printing, and probably only print a single newline between words.
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
add a comment |
You are not showing how you are calling either of these functions, but something like
def scramble_words(lst, n=15):
"""
Shuffle words in lst
Print n reordered words by newline
"""
random.shuffle(lst)
print()
print("nn".join(lst[:n]))
# ...
args = get_args()
scramble_words(new_list, args.noutput)
A better design would have the caller do the printing, and probably only print a single newline between words.
You are not showing how you are calling either of these functions, but something like
def scramble_words(lst, n=15):
"""
Shuffle words in lst
Print n reordered words by newline
"""
random.shuffle(lst)
print()
print("nn".join(lst[:n]))
# ...
args = get_args()
scramble_words(new_list, args.noutput)
A better design would have the caller do the printing, and probably only print a single newline between words.
answered Jan 1 at 23:40
tripleeetripleee
94.1k13132186
94.1k13132186
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
add a comment |
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
Ah, sorry for omitting the details there. Yup, I'm going to use scramble_words(new_list, args.noutput) here. And ha, thanks for the memo on the printing! Definitely felt silly printing a blank line at first...
– J. Cheng
Jan 2 at 0:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999537%2fusing-argparse-to-pass-in-number-of-output-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What answer other than the obvious "pass the value to the function which needs it" do you expect?
– tripleee
Jan 1 at 23:05
More specifically, my question is how I would set a variable from my option. I was going through argparse doc and testing what I wrote, but am unable to add a variable within the parser.add_argument part. Will keep reading the doc to see if I can find the answer...
– J. Cheng
Jan 1 at 23:27
get_args().noutput
contains the value. You're not showing how you are calling either of these functions so we can't tell exactly what you are going wrong. Thescramble_words
definition should obviously be changed to accept an argument (perhaps optional).– tripleee
Jan 1 at 23:33
I see! Yup will add the argument to scramble_words and play with this. Thanks so much!
– J. Cheng
Jan 1 at 23:38
1
During debugging it's a good idea to print the result of
parse_args()
. That will give you a clearer idea of what the parser has done. It shows the names and values of the attributes it has parsed.– hpaulj
Jan 2 at 2:34