How to use argparse without using dest variable? [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Python argparse command line flags without arguments

    3 answers




I want to use argparse to get option to my program. Here is my sample program:



from argparse import ArgumentParser

parser = ArgumentParser()

print("enter the numbers:")

a=int(input("number 1:"))

b=int(input("number 2:"))

parser.add_argument('-a','--add')

parser.add_argument('-s','--sub')

options = parser.parse_args()

if options:

c=a+b

if options.d:

c=a-b

print(c)


it gives the output correctly if I use



python file.pu -a 1


But I don't want to give value like 1 in compilation. What I want is



python file.py -a     


that performs addition.



python file.py -s


that performs subtraction.



How to change the code for it?










share|improve this question















marked as duplicate by jonrsharpe python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 8:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    -1
    down vote

    favorite













    This question already has an answer here:




    • Python argparse command line flags without arguments

      3 answers




    I want to use argparse to get option to my program. Here is my sample program:



    from argparse import ArgumentParser

    parser = ArgumentParser()

    print("enter the numbers:")

    a=int(input("number 1:"))

    b=int(input("number 2:"))

    parser.add_argument('-a','--add')

    parser.add_argument('-s','--sub')

    options = parser.parse_args()

    if options:

    c=a+b

    if options.d:

    c=a-b

    print(c)


    it gives the output correctly if I use



    python file.pu -a 1


    But I don't want to give value like 1 in compilation. What I want is



    python file.py -a     


    that performs addition.



    python file.py -s


    that performs subtraction.



    How to change the code for it?










    share|improve this question















    marked as duplicate by jonrsharpe python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 20 at 8:35


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite












      This question already has an answer here:




      • Python argparse command line flags without arguments

        3 answers




      I want to use argparse to get option to my program. Here is my sample program:



      from argparse import ArgumentParser

      parser = ArgumentParser()

      print("enter the numbers:")

      a=int(input("number 1:"))

      b=int(input("number 2:"))

      parser.add_argument('-a','--add')

      parser.add_argument('-s','--sub')

      options = parser.parse_args()

      if options:

      c=a+b

      if options.d:

      c=a-b

      print(c)


      it gives the output correctly if I use



      python file.pu -a 1


      But I don't want to give value like 1 in compilation. What I want is



      python file.py -a     


      that performs addition.



      python file.py -s


      that performs subtraction.



      How to change the code for it?










      share|improve this question
















      This question already has an answer here:




      • Python argparse command line flags without arguments

        3 answers




      I want to use argparse to get option to my program. Here is my sample program:



      from argparse import ArgumentParser

      parser = ArgumentParser()

      print("enter the numbers:")

      a=int(input("number 1:"))

      b=int(input("number 2:"))

      parser.add_argument('-a','--add')

      parser.add_argument('-s','--sub')

      options = parser.parse_args()

      if options:

      c=a+b

      if options.d:

      c=a-b

      print(c)


      it gives the output correctly if I use



      python file.pu -a 1


      But I don't want to give value like 1 in compilation. What I want is



      python file.py -a     


      that performs addition.



      python file.py -s


      that performs subtraction.



      How to change the code for it?





      This question already has an answer here:




      • Python argparse command line flags without arguments

        3 answers








      python python-3.x python-3.6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 8:34









      jonrsharpe

      76.4k10100206




      76.4k10100206










      asked Nov 20 at 8:33









      joker

      12




      12




      marked as duplicate by jonrsharpe python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 8:35


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by jonrsharpe python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 8:35


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can use:



          action='store_true'


          ...in the following:



          parser.add_argument('-a', '-add', action='store_true')





          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You can use:



            action='store_true'


            ...in the following:



            parser.add_argument('-a', '-add', action='store_true')





            share|improve this answer

























              up vote
              0
              down vote













              You can use:



              action='store_true'


              ...in the following:



              parser.add_argument('-a', '-add', action='store_true')





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can use:



                action='store_true'


                ...in the following:



                parser.add_argument('-a', '-add', action='store_true')





                share|improve this answer












                You can use:



                action='store_true'


                ...in the following:



                parser.add_argument('-a', '-add', action='store_true')






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 8:39









                Adam

                1067




                1067















                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'